Skip to content

Commit 20a9ed5

Browse files
fix: #370 provide means of handling textures form CPU
fix: Add `Kernel.features.channelCount` to each kernel, and unit test fix: Was curios for code relating to https://github.com/gpujs/gpu.js/wiki/Quick-Concepts and so added a unit tests for matrix-multiply-precision.js
1 parent a0a9258 commit 20a9ed5

File tree

15 files changed

+831
-37
lines changed

15 files changed

+831
-37
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
GPU.js is a JavaScript Acceleration library for GPGPU (General purpose computing on GPUs) in JavaScript for Web and Node.
66
GPU.js automatically transpiles simple JavaScript functions into shader language and compiles them so they run on your GPU.
77
In case a GPU is not available, the functions will still run in regular JavaScript.
8+
For some more quick concepts, see [Quick Concepts](https://github.com/gpujs/gpu.js/wiki/Quick-Concepts) on the wiki.
89

910

1011
[![Join the chat at https://gitter.im/gpujs/gpu.js](https://badges.gitter.im/gpujs/gpu.js.svg)](https://gitter.im/gpujs/gpu.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

bin/gpu-browser-core.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* GPU Accelerated JavaScript
66
*
77
* @version 2.0.0-rc.14
8-
* @date Wed May 22 2019 08:21:57 GMT-0400 (Eastern Daylight Time)
8+
* @date Wed May 22 2019 15:57:55 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -1256,15 +1256,38 @@ class CPUKernel extends Kernel {
12561256
_processArguments() {
12571257
const result = [];
12581258
for (let i = 0; i < this.argumentTypes.length; i++) {
1259+
const variableName = `user_${this.argumentNames[i]}`;
12591260
switch (this.argumentTypes[i]) {
12601261
case 'HTMLImage':
1261-
result.push(` user_${this.argumentNames[i]} = this._imageTo2DArray(user_${this.argumentNames[i]});\n`);
1262+
result.push(` ${variableName} = this._imageTo2DArray(${variableName});\n`);
12621263
break;
12631264
case 'HTMLImageArray':
1264-
result.push(` user_${this.argumentNames[i]} = this._imageTo3DArray(user_${this.argumentNames[i]});\n`);
1265+
result.push(` ${variableName} = this._imageTo3DArray(${variableName});\n`);
12651266
break;
12661267
case 'Input':
1267-
result.push(` user_${this.argumentNames[i]} = user_${this.argumentNames[i]}.value;\n`);
1268+
result.push(` ${variableName} = ${variableName}.value;\n`);
1269+
break;
1270+
case 'ArrayTexture(1)':
1271+
case 'ArrayTexture(2)':
1272+
case 'ArrayTexture(3)':
1273+
case 'ArrayTexture(4)':
1274+
case 'NumberTexture':
1275+
case 'MemoryOptimizedNumberTexture':
1276+
result.push(`
1277+
if (${variableName}.toArray) {
1278+
if (!_this.textureCache) {
1279+
_this.textureCache = [];
1280+
_this.arrayCache = [];
1281+
}
1282+
const textureIndex = _this.textureCache.indexOf(${variableName});
1283+
if (textureIndex !== -1) {
1284+
${variableName} = _this.arrayCache[textureIndex];
1285+
} else {
1286+
_this.textureCache.push(${variableName});
1287+
${variableName} = ${variableName}.toArray();
1288+
_this.arrayCache.push(${variableName});
1289+
}
1290+
}`);
12681291
break;
12691292
}
12701293
}
@@ -3508,7 +3531,6 @@ class GLKernel extends Kernel {
35083531
case 'Float':
35093532
case 'Number':
35103533
case 'Integer':
3511-
;
35123534
if (this.output[2] > 0) {
35133535
this.TextureConstructor = GLTextureUnsigned3D;
35143536
this.renderStrategy = renderStrategy.PackedPixelTo3DFloat;
@@ -4468,7 +4490,8 @@ class HeadlessGLKernel extends WebGLKernel {
44684490
isIntegerDivisionAccurate: this.getIsIntegerDivisionAccurate(),
44694491
isTextureFloat: this.getIsTextureFloat(),
44704492
isDrawBuffers,
4471-
kernelMap: isDrawBuffers
4493+
kernelMap: isDrawBuffers,
4494+
channelCount: this.getChannelCount(),
44724495
});
44734496
}
44744497

@@ -4480,6 +4503,12 @@ class HeadlessGLKernel extends WebGLKernel {
44804503
return Boolean(testExtensions.WEBGL_draw_buffers);
44814504
}
44824505

4506+
static getChannelCount() {
4507+
return testExtensions.WEBGL_draw_buffers ?
4508+
testContext.getParameter(testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL) :
4509+
1;
4510+
}
4511+
44834512
static get testCanvas() {
44844513
return testCanvas;
44854514
}
@@ -7274,7 +7303,9 @@ class WebGLKernel extends GLKernel {
72747303
}
72757304

72767305
static getChannelCount() {
7277-
return testExtensions.WEBGL_draw_buffers ? testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL : 1;
7306+
return testExtensions.WEBGL_draw_buffers ?
7307+
testContext.getParameter(testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL) :
7308+
1;
72787309
}
72797310

72807311
static lookupKernelValueType(type, dynamic, precision) {
@@ -9227,6 +9258,7 @@ class WebGL2Kernel extends WebGLKernel {
92279258
isIntegerDivisionAccurate: this.getIsIntegerDivisionAccurate(),
92289259
kernelMap: true,
92299260
isTextureFloat: true,
9261+
channelCount: this.getChannelCount(),
92309262
});
92319263
}
92329264

@@ -9238,6 +9270,10 @@ class WebGL2Kernel extends WebGLKernel {
92389270
return super.getIsIntegerDivisionAccurate();
92399271
}
92409272

9273+
static getChannelCount() {
9274+
return testContext.getParameter(testContext.MAX_DRAW_BUFFERS);
9275+
}
9276+
92419277
static lookupKernelValueType(type, dynamic, precision) {
92429278
return lookupKernelValueType(type, dynamic, precision);
92439279
}

bin/gpu-browser-core.min.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* GPU Accelerated JavaScript
66
*
77
* @version 2.0.0-rc.14
8-
* @date Wed May 22 2019 08:21:59 GMT-0400 (Eastern Daylight Time)
8+
* @date Wed May 22 2019 15:57:57 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -18,7 +18,7 @@
1818
* GPU Accelerated JavaScript
1919
*
2020
* @version 2.0.0-rc.14
21-
* @date Wed May 22 2019 08:21:57 GMT-0400 (Eastern Daylight Time)
21+
* @date Wed May 22 2019 15:57:55 GMT-0400 (Eastern Daylight Time)
2222
*
2323
* @license MIT
2424
* The MIT License
@@ -1269,15 +1269,38 @@ class CPUKernel extends Kernel {
12691269
_processArguments() {
12701270
const result = [];
12711271
for (let i = 0; i < this.argumentTypes.length; i++) {
1272+
const variableName = `user_${this.argumentNames[i]}`;
12721273
switch (this.argumentTypes[i]) {
12731274
case 'HTMLImage':
1274-
result.push(` user_${this.argumentNames[i]} = this._imageTo2DArray(user_${this.argumentNames[i]});\n`);
1275+
result.push(` ${variableName} = this._imageTo2DArray(${variableName});\n`);
12751276
break;
12761277
case 'HTMLImageArray':
1277-
result.push(` user_${this.argumentNames[i]} = this._imageTo3DArray(user_${this.argumentNames[i]});\n`);
1278+
result.push(` ${variableName} = this._imageTo3DArray(${variableName});\n`);
12781279
break;
12791280
case 'Input':
1280-
result.push(` user_${this.argumentNames[i]} = user_${this.argumentNames[i]}.value;\n`);
1281+
result.push(` ${variableName} = ${variableName}.value;\n`);
1282+
break;
1283+
case 'ArrayTexture(1)':
1284+
case 'ArrayTexture(2)':
1285+
case 'ArrayTexture(3)':
1286+
case 'ArrayTexture(4)':
1287+
case 'NumberTexture':
1288+
case 'MemoryOptimizedNumberTexture':
1289+
result.push(`
1290+
if (${variableName}.toArray) {
1291+
if (!_this.textureCache) {
1292+
_this.textureCache = [];
1293+
_this.arrayCache = [];
1294+
}
1295+
const textureIndex = _this.textureCache.indexOf(${variableName});
1296+
if (textureIndex !== -1) {
1297+
${variableName} = _this.arrayCache[textureIndex];
1298+
} else {
1299+
_this.textureCache.push(${variableName});
1300+
${variableName} = ${variableName}.toArray();
1301+
_this.arrayCache.push(${variableName});
1302+
}
1303+
}`);
12811304
break;
12821305
}
12831306
}
@@ -3521,7 +3544,6 @@ class GLKernel extends Kernel {
35213544
case 'Float':
35223545
case 'Number':
35233546
case 'Integer':
3524-
;
35253547
if (this.output[2] > 0) {
35263548
this.TextureConstructor = GLTextureUnsigned3D;
35273549
this.renderStrategy = renderStrategy.PackedPixelTo3DFloat;
@@ -4481,7 +4503,8 @@ class HeadlessGLKernel extends WebGLKernel {
44814503
isIntegerDivisionAccurate: this.getIsIntegerDivisionAccurate(),
44824504
isTextureFloat: this.getIsTextureFloat(),
44834505
isDrawBuffers,
4484-
kernelMap: isDrawBuffers
4506+
kernelMap: isDrawBuffers,
4507+
channelCount: this.getChannelCount(),
44854508
});
44864509
}
44874510

@@ -4493,6 +4516,12 @@ class HeadlessGLKernel extends WebGLKernel {
44934516
return Boolean(testExtensions.WEBGL_draw_buffers);
44944517
}
44954518

4519+
static getChannelCount() {
4520+
return testExtensions.WEBGL_draw_buffers ?
4521+
testContext.getParameter(testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL) :
4522+
1;
4523+
}
4524+
44964525
static get testCanvas() {
44974526
return testCanvas;
44984527
}
@@ -7287,7 +7316,9 @@ class WebGLKernel extends GLKernel {
72877316
}
72887317

72897318
static getChannelCount() {
7290-
return testExtensions.WEBGL_draw_buffers ? testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL : 1;
7319+
return testExtensions.WEBGL_draw_buffers ?
7320+
testContext.getParameter(testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL) :
7321+
1;
72917322
}
72927323

72937324
static lookupKernelValueType(type, dynamic, precision) {
@@ -9240,6 +9271,7 @@ class WebGL2Kernel extends WebGLKernel {
92409271
isIntegerDivisionAccurate: this.getIsIntegerDivisionAccurate(),
92419272
kernelMap: true,
92429273
isTextureFloat: true,
9274+
channelCount: this.getChannelCount(),
92439275
});
92449276
}
92459277

@@ -9251,6 +9283,10 @@ class WebGL2Kernel extends WebGLKernel {
92519283
return super.getIsIntegerDivisionAccurate();
92529284
}
92539285

9286+
static getChannelCount() {
9287+
return testContext.getParameter(testContext.MAX_DRAW_BUFFERS);
9288+
}
9289+
92549290
static lookupKernelValueType(type, dynamic, precision) {
92559291
return lookupKernelValueType(type, dynamic, precision);
92569292
}

bin/gpu-browser.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* GPU Accelerated JavaScript
66
*
77
* @version 2.0.0-rc.14
8-
* @date Wed May 22 2019 08:21:57 GMT-0400 (Eastern Daylight Time)
8+
* @date Wed May 22 2019 15:57:55 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -6020,15 +6020,38 @@ class CPUKernel extends Kernel {
60206020
_processArguments() {
60216021
const result = [];
60226022
for (let i = 0; i < this.argumentTypes.length; i++) {
6023+
const variableName = `user_${this.argumentNames[i]}`;
60236024
switch (this.argumentTypes[i]) {
60246025
case 'HTMLImage':
6025-
result.push(` user_${this.argumentNames[i]} = this._imageTo2DArray(user_${this.argumentNames[i]});\n`);
6026+
result.push(` ${variableName} = this._imageTo2DArray(${variableName});\n`);
60266027
break;
60276028
case 'HTMLImageArray':
6028-
result.push(` user_${this.argumentNames[i]} = this._imageTo3DArray(user_${this.argumentNames[i]});\n`);
6029+
result.push(` ${variableName} = this._imageTo3DArray(${variableName});\n`);
60296030
break;
60306031
case 'Input':
6031-
result.push(` user_${this.argumentNames[i]} = user_${this.argumentNames[i]}.value;\n`);
6032+
result.push(` ${variableName} = ${variableName}.value;\n`);
6033+
break;
6034+
case 'ArrayTexture(1)':
6035+
case 'ArrayTexture(2)':
6036+
case 'ArrayTexture(3)':
6037+
case 'ArrayTexture(4)':
6038+
case 'NumberTexture':
6039+
case 'MemoryOptimizedNumberTexture':
6040+
result.push(`
6041+
if (${variableName}.toArray) {
6042+
if (!_this.textureCache) {
6043+
_this.textureCache = [];
6044+
_this.arrayCache = [];
6045+
}
6046+
const textureIndex = _this.textureCache.indexOf(${variableName});
6047+
if (textureIndex !== -1) {
6048+
${variableName} = _this.arrayCache[textureIndex];
6049+
} else {
6050+
_this.textureCache.push(${variableName});
6051+
${variableName} = ${variableName}.toArray();
6052+
_this.arrayCache.push(${variableName});
6053+
}
6054+
}`);
60326055
break;
60336056
}
60346057
}
@@ -8272,7 +8295,6 @@ class GLKernel extends Kernel {
82728295
case 'Float':
82738296
case 'Number':
82748297
case 'Integer':
8275-
;
82768298
if (this.output[2] > 0) {
82778299
this.TextureConstructor = GLTextureUnsigned3D;
82788300
this.renderStrategy = renderStrategy.PackedPixelTo3DFloat;
@@ -9232,7 +9254,8 @@ class HeadlessGLKernel extends WebGLKernel {
92329254
isIntegerDivisionAccurate: this.getIsIntegerDivisionAccurate(),
92339255
isTextureFloat: this.getIsTextureFloat(),
92349256
isDrawBuffers,
9235-
kernelMap: isDrawBuffers
9257+
kernelMap: isDrawBuffers,
9258+
channelCount: this.getChannelCount(),
92369259
});
92379260
}
92389261

@@ -9244,6 +9267,12 @@ class HeadlessGLKernel extends WebGLKernel {
92449267
return Boolean(testExtensions.WEBGL_draw_buffers);
92459268
}
92469269

9270+
static getChannelCount() {
9271+
return testExtensions.WEBGL_draw_buffers ?
9272+
testContext.getParameter(testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL) :
9273+
1;
9274+
}
9275+
92479276
static get testCanvas() {
92489277
return testCanvas;
92499278
}
@@ -12038,7 +12067,9 @@ class WebGLKernel extends GLKernel {
1203812067
}
1203912068

1204012069
static getChannelCount() {
12041-
return testExtensions.WEBGL_draw_buffers ? testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL : 1;
12070+
return testExtensions.WEBGL_draw_buffers ?
12071+
testContext.getParameter(testExtensions.WEBGL_draw_buffers.MAX_DRAW_BUFFERS_WEBGL) :
12072+
1;
1204212073
}
1204312074

1204412075
static lookupKernelValueType(type, dynamic, precision) {
@@ -13991,6 +14022,7 @@ class WebGL2Kernel extends WebGLKernel {
1399114022
isIntegerDivisionAccurate: this.getIsIntegerDivisionAccurate(),
1399214023
kernelMap: true,
1399314024
isTextureFloat: true,
14025+
channelCount: this.getChannelCount(),
1399414026
});
1399514027
}
1399614028

@@ -14002,6 +14034,10 @@ class WebGL2Kernel extends WebGLKernel {
1400214034
return super.getIsIntegerDivisionAccurate();
1400314035
}
1400414036

14037+
static getChannelCount() {
14038+
return testContext.getParameter(testContext.MAX_DRAW_BUFFERS);
14039+
}
14040+
1400514041
static lookupKernelValueType(type, dynamic, precision) {
1400614042
return lookupKernelValueType(type, dynamic, precision);
1400714043
}

0 commit comments

Comments
 (0)