Skip to content

Commit 67300c3

Browse files
fix: Use a loop rather than a map to save on a single function declaration
fix: Add a test for `utils.getKernelTextureSize` newly renamed fix: WebGL2 to use preferred `gl.texStorage2D()` where possible Also, found an issue with "Array(3)", and it turns out, I need to use `gl.RGBA32F` there
1 parent db20ffc commit 67300c3

File tree

12 files changed

+381
-307
lines changed

12 files changed

+381
-307
lines changed

bin/gpu-browser-core.js

Lines changed: 54 additions & 58 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 15:57:55 GMT-0400 (Eastern Daylight Time)
8+
* @date Thu May 23 2019 14:25:29 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -4011,10 +4011,10 @@ class GLKernel extends Kernel {
40114011
super.setOutput(output);
40124012
if (this.program) {
40134013
this.threadDim = [this.output[0], this.output[1] || 1, this.output[2] || 1];
4014-
this.texSize = utils.dimToTexSize({
4015-
floatTextures: this.optimizeFloatMemory,
4016-
floatOutput: this.precision === 'single',
4017-
}, this.threadDim, true);
4014+
this.texSize = utils.getKernelTextureSize({
4015+
optimizeFloatMemory: this.optimizeFloatMemory,
4016+
precision: this.precision,
4017+
}, this.output);
40184018
const { context: gl } = this;
40194019
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
40204020
this.updateMaxTexSize();
@@ -7425,10 +7425,10 @@ class WebGLKernel extends GLKernel {
74257425

74267426
validateSettings() {
74277427
if (!this.validate) {
7428-
this.texSize = utils.dimToTexSize({
7429-
floatTextures: this.optimizeFloatMemory,
7430-
floatOutput: this.precision === 'single',
7431-
}, this.output, true);
7428+
this.texSize = utils.getKernelTextureSize({
7429+
optimizeFloatMemory: this.optimizeFloatMemory,
7430+
precision: this.precision,
7431+
}, this.output);
74327432
return;
74337433
}
74347434

@@ -7484,10 +7484,10 @@ class WebGLKernel extends GLKernel {
74847484
this.precision = 'single';
74857485
}
74867486

7487-
this.texSize = utils.dimToTexSize({
7488-
floatTextures: this.floatTextures,
7489-
floatOutput: this.precision === 'single'
7490-
}, this.output, true);
7487+
this.texSize = utils.getKernelTextureSize({
7488+
optimizeFloatMemory: this.optimizeFloatMemory,
7489+
precision: this.precision,
7490+
}, this.output);
74917491
}
74927492

74937493
updateMaxTexSize() {
@@ -9316,10 +9316,10 @@ class WebGL2Kernel extends WebGLKernel {
93169316

93179317
validateSettings() {
93189318
if (!this.validate) {
9319-
this.texSize = utils.dimToTexSize({
9320-
floatTextures: this.optimizeFloatMemory,
9321-
floatOutput: this.precision === 'single',
9322-
}, this.output, true);
9319+
this.texSize = utils.getKernelTextureSize({
9320+
optimizeFloatMemory: this.optimizeFloatMemory,
9321+
precision: this.precision,
9322+
}, this.output);
93239323
return;
93249324
}
93259325

@@ -9377,14 +9377,10 @@ class WebGL2Kernel extends WebGLKernel {
93779377
this.precision = 'single';
93789378
}
93799379

9380-
this.texSize = utils.dimToTexSize({
9381-
floatTextures: !this.optimizeFloatMemory,
9382-
floatOutput: this.precision === 'single',
9383-
}, this.output, true);
9384-
9385-
if (this.precision === 'single') {
9386-
this.context.getExtension('EXT_color_buffer_float');
9387-
}
9380+
this.texSize = utils.getKernelTextureSize({
9381+
optimizeFloatMemory: this.optimizeFloatMemory,
9382+
precision: this.precision,
9383+
}, this.output);
93889384
}
93899385

93909386
translateSource() {
@@ -9487,25 +9483,23 @@ class WebGL2Kernel extends WebGLKernel {
94879483
case 'Float':
94889484
case 'Integer':
94899485
if (this.optimizeFloatMemory) {
9490-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, texSize[0], texSize[1], 0, gl.RGBA, gl.FLOAT, null);
9486+
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, texSize[0], texSize[1]);
94919487
} else {
9492-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.R32F, texSize[0], texSize[1], 0, gl.RED, gl.FLOAT, null);
9488+
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R32F, texSize[0], texSize[1]);
94939489
}
94949490
break;
94959491
case 'Array(2)':
9496-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RG32F, texSize[0], texSize[1], 0, gl.RG, gl.FLOAT, null);
9497-
break;
9498-
case 'Array(3)':
9499-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB32F, texSize[0], texSize[1], 0, gl.RGB, gl.FLOAT, null);
9492+
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RG32F, texSize[0], texSize[1]);
95009493
break;
9494+
case 'Array(3)':
95019495
case 'Array(4)':
9502-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, texSize[0], texSize[1], 0, gl.RGBA, gl.FLOAT, null);
9496+
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, texSize[0], texSize[1]);
95039497
break;
95049498
default:
95059499
throw new Error('Unhandled return type');
95069500
}
95079501
} else {
9508-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, texSize[0], texSize[1], 0, gl.RGBA, gl.FLOAT, null);
9502+
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, texSize[0], texSize[1]);
95099503
}
95109504
} else {
95119505
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize[0], texSize[1], 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
@@ -9802,6 +9796,7 @@ class WebGL2Kernel extends WebGLKernel {
98029796
module.exports = {
98039797
WebGL2Kernel
98049798
};
9799+
98059800
},{"../../utils":88,"../function-builder":8,"../web-gl/kernel":55,"./fragment-shader":57,"./function-node":58,"./kernel-value-maps":59,"./vertex-shader":80}],80:[function(require,module,exports){
98069801
const vertexShader = `#version 300 es
98079802
precision highp float;
@@ -10331,33 +10326,34 @@ function kernelRunShortcut(kernel) {
1033110326
kernel = replacementKernel;
1033210327
};
1033310328

10334-
utils
10335-
.allPropertiesOf(kernel)
10336-
.forEach((key) => {
10337-
if (key[0] === '_' && key[1] === '_') return;
10338-
if (typeof kernel[key] === 'function') {
10339-
if (key.substring(0, 3) === 'add' || key.substring(0, 3) === 'set') {
10340-
shortcut[key] = function() {
10341-
kernel[key].apply(kernel, arguments);
10342-
return shortcut;
10343-
};
10344-
} else if (key === 'requestFallback') {
10345-
const requestFallback = kernel[key].bind(kernel);
10346-
shortcut[key] = () => {
10347-
kernel = requestFallback();
10329+
const properties = utils.allPropertiesOf(kernel);
10330+
for (let i = 0; i < properties.length; i++) {
10331+
const property = properties[i];
10332+
if (property[0] === '_' && property[1] === '_') continue;
10333+
if (typeof kernel[property] === 'function') {
10334+
if (property.substring(0, 3) === 'add' || property.substring(0, 3) === 'set') {
10335+
shortcut[property] = function() {
10336+
kernel[property].apply(kernel, arguments);
10337+
return shortcut;
10338+
};
10339+
} else {
10340+
if (property === 'toString') {
10341+
shortcut.toString = function() {
10342+
return kernel.toString.apply(kernel, arguments);
1034810343
};
1034910344
} else {
10350-
shortcut[key] = kernel[key].bind(kernel);
10345+
shortcut[property] = kernel[property].bind(kernel);
1035110346
}
10352-
} else {
10353-
shortcut.__defineGetter__(key, () => {
10354-
return kernel[key];
10355-
});
10356-
shortcut.__defineSetter__(key, (value) => {
10357-
kernel[key] = value;
10358-
});
1035910347
}
10360-
});
10348+
} else {
10349+
shortcut.__defineGetter__(property, () => {
10350+
return kernel[property];
10351+
});
10352+
shortcut.__defineSetter__(property, (value) => {
10353+
kernel[property] = value;
10354+
});
10355+
}
10356+
}
1036110357

1036210358
shortcut.kernel = kernel;
1036310359

@@ -10556,11 +10552,11 @@ const utils = {
1055610552
},
1055710553

1055810554

10559-
dimToTexSize(opt, dimensions, output) {
10555+
getKernelTextureSize(settings, dimensions) {
1056010556
let [w, h, d] = dimensions;
1056110557
let texelCount = (w || 1) * (h || 1) * (d || 1);
1056210558

10563-
if (opt.floatTextures && (!output || opt.precision === 'single')) {
10559+
if (settings.optimizeFloatMemory && settings.precision === 'single') {
1056410560
w = texelCount = Math.ceil(texelCount / 4);
1056510561
}
1056610562
if (h > 1 && w * h === texelCount) {

0 commit comments

Comments
 (0)