Skip to content

Commit

Permalink
Merge pull request #256 from gpujs/255-ouput-immutable
Browse files Browse the repository at this point in the history
255 - implement new option to prevent overwrite of output texture
  • Loading branch information
robertleeplummerjr authored Feb 26, 2018
2 parents e0d376f + 6c62bb2 commit a2f3158
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 64 deletions.
6 changes: 3 additions & 3 deletions bin/gpu-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*
* GPU Accelerated JavaScript
*
* @version 1.0.0-rc.10
* @date Sat Dec 23 2017 10:30:08 GMT-0500 (EST)
* @version 1.0.0
* @date Mon Feb 12 2018 12:08:17 GMT-0500 (EST)
*
* @license MIT
* The MIT License
*
* Copyright (c) 2017 gpu.js Team
* Copyright (c) 2018 gpu.js Team
*/
"use strict";(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
Expand Down
6 changes: 3 additions & 3 deletions bin/gpu-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions bin/gpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*
* GPU Accelerated JavaScript
*
* @version 1.0.0-rc.10
* @date Sat Dec 23 2017 10:30:09 GMT-0500 (EST)
* @version 1.0.0
* @date Mon Feb 12 2018 12:08:17 GMT-0500 (EST)
*
* @license MIT
* The MIT License
*
* Copyright (c) 2017 gpu.js Team
* Copyright (c) 2018 gpu.js Team
*/
"use strict";(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
Expand Down Expand Up @@ -1518,6 +1518,7 @@ module.exports = function () {
this.wraparound = null;
this.hardcodeConstants = null;
this.outputToTexture = null;
this.outputImmutable = null;
this.texSize = null;
this._canvas = null;
this._webGl = null;
Expand Down Expand Up @@ -1650,6 +1651,12 @@ module.exports = function () {
this.outputToTexture = flag;
return this;
}
}, {
key: 'setOutputImmutable',
value: function setOutputImmutable(flag) {
this.outputImmutable = flag;
return this;
}


}, {
Expand Down Expand Up @@ -3019,6 +3026,9 @@ module.exports = function (_KernelBase) {
}

gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
if (this.outputImmutable) {
this.setupOutputTexture();
}
var outputTexture = this.outputTexture;
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);

Expand Down Expand Up @@ -3302,7 +3312,7 @@ module.exports = function (_KernelBase) {

var _size2 = inputTexture.size;

if (inputTexture.texture === this.outputTexture) {
if (!this.outputImmutable && inputTexture.texture === this.outputTexture) {
this.setupOutputTexture();
}

Expand Down
18 changes: 9 additions & 9 deletions bin/gpu.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions dist/backend/kernel-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function () {
this.wraparound = null;
this.hardcodeConstants = null;
this.outputToTexture = null;
this.outputImmutable = null;
this.texSize = null;
this._canvas = null;
this._webGl = null;
Expand Down Expand Up @@ -231,6 +232,12 @@ module.exports = function () {
this.outputToTexture = flag;
return this;
}
}, {
key: 'setOutputImmutable',
value: function setOutputImmutable(flag) {
this.outputImmutable = flag;
return this;
}

/**
* @memberOf BaseKernel#
Expand Down
5 changes: 4 additions & 1 deletion dist/backend/web-gl/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ module.exports = function (_KernelBase) {

gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
//the call to this._addArgument may rewrite the outputTexture, keep this here
if (this.outputImmutable) {
this.setupOutputTexture();
}
var outputTexture = this.outputTexture;
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);

Expand Down Expand Up @@ -723,7 +726,7 @@ module.exports = function (_KernelBase) {

var _size2 = inputTexture.size;

if (inputTexture.texture === this.outputTexture) {
if (!this.outputImmutable && inputTexture.texture === this.outputTexture) {
this.setupOutputTexture();
}

Expand Down
6 changes: 6 additions & 0 deletions src/backend/kernel-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = class BaseKernel {
this.wraparound = null;
this.hardcodeConstants = null;
this.outputToTexture = null;
this.outputImmutable = null;
this.texSize = null;
this._canvas = null;
this._webGl = null;
Expand Down Expand Up @@ -201,6 +202,11 @@ module.exports = class BaseKernel {
return this;
}

setOutputImmutable(flag) {
this.outputImmutable = flag;
return this;
}

/**
* @memberOf BaseKernel#
* @function
Expand Down
27 changes: 15 additions & 12 deletions src/backend/web-gl/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ module.exports = class WebGLKernel extends KernelBase {
*/
validateOptions() {
const isFloatReadPixel = utils.isFloatReadPixelsSupported();
if (this.floatTextures === true && !utils.OES_texture_float) {
throw new Error('Float textures are not supported on this browser');
} else if (this.floatOutput === true && this.floatOutputForce !== true && !isFloatReadPixel) {
throw new Error('Float texture outputs are not supported on this browser');
} else if (this.floatTextures === undefined && utils.OES_texture_float) {
this.floatTextures = true;
this.floatOutput = isFloatReadPixel;
}

if (!this.output || this.output.length === 0) {
if (this.floatTextures === true && !utils.OES_texture_float) {
throw new Error('Float textures are not supported on this browser');
} else if (this.floatOutput === true && this.floatOutputForce !== true && !isFloatReadPixel) {
throw new Error('Float texture outputs are not supported on this browser');
} else if (this.floatTextures === undefined && utils.OES_texture_float) {
this.floatTextures = true;
this.floatOutput = isFloatReadPixel;
}

if (!this.output || this.output.length === 0) {
if (arguments.length !== 1) {
throw new Error('Auto output only supported for kernels with only one input');
}
Expand All @@ -103,7 +103,7 @@ module.exports = class WebGLKernel extends KernelBase {
}

if (this.floatOutput) {
this.floatOutput = false;
this.floatOutput = false;
console.warn('Cannot use graphical mode and float output at the same time');
}

Expand Down Expand Up @@ -300,6 +300,9 @@ module.exports = class WebGLKernel extends KernelBase {

gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
//the call to this._addArgument may rewrite the outputTexture, keep this here
if (this.outputImmutable) {
this.setupOutputTexture();
}
const outputTexture = this.outputTexture;
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);

Expand Down Expand Up @@ -674,7 +677,7 @@ module.exports = class WebGLKernel extends KernelBase {

const size = inputTexture.size;

if (inputTexture.texture === this.outputTexture) {
if (!this.outputImmutable && inputTexture.texture === this.outputTexture) {
this.setupOutputTexture();
}

Expand Down
48 changes: 24 additions & 24 deletions src/core/gpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class GPU extends GPUCore {
let mode = settings.mode;
let detectedMode;
if (!utils.isWebGlSupported()) {
if (mode && mode !== 'cpu') {
throw new Error(`A requested mode of "${ mode }" and is not supported`);
} else {
console.warn('Warning: gpu not supported, falling back to cpu support');
detectedMode = 'cpu';
}
} else {
detectedMode = mode || 'gpu';
}
if (mode && mode !== 'cpu') {
throw new Error(`A requested mode of "${ mode }" and is not supported`);
} else {
console.warn('Warning: gpu not supported, falling back to cpu support');
detectedMode = 'cpu';
}
} else {
detectedMode = mode || 'gpu';
}

this.kernels = [];

Expand All @@ -43,21 +43,21 @@ class GPU extends GPUCore {
webGl: this._webGl
};

switch (detectedMode) {
case 'cpu':
this._runner = new CPURunner(runnerSettings);
break;
case 'webgl': // for testing
case 'gpu':
this._runner = new WebGLRunner(runnerSettings);
break;
case 'webgl-validator': // for internal
this._runner = new WebGLRunner(runnerSettings);
this._runner.Kernel = WebGLValidatorKernel;
break;
default:
throw new Error(`"${ mode }" mode is not defined`);
}
switch (detectedMode) {
case 'cpu':
this._runner = new CPURunner(runnerSettings);
break;
case 'webgl': // for testing
case 'gpu':
this._runner = new WebGLRunner(runnerSettings);
break;
case 'webgl-validator': // for internal
this._runner = new WebGLRunner(runnerSettings);
this._runner.Kernel = WebGLValidatorKernel;
break;
default:
throw new Error(`"${ mode }" mode is not defined`);
}
}
/**
*
Expand Down
16 changes: 8 additions & 8 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ const _systemEndianness = (() => {
let _isFloatReadPixelsSupported = null;

let _isMixedIdentifiersSupported = (() => {
try {
(new Function('let i = 1; const j = 1;'))();
return true;
} catch (e) {
return false;
}
try {
(new Function('let i = 1; const j = 1;'))();
return true;
} catch (e) {
return false;
}
})();

class Utils extends UtilsCore {
Expand Down Expand Up @@ -326,8 +326,8 @@ class Utils extends UtilsCore {
}

static isMixedIdentifiersSupported() {
return _isMixedIdentifiersSupported;
}
return _isMixedIdentifiersSupported;
}

static dimToTexSize(opt, dimensions, output) {
let numTexels = dimensions[0];
Expand Down

0 comments on commit a2f3158

Please sign in to comment.