Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,13 +895,14 @@ This is a list of the supported ones:
* `Math.min()`
* `Math.pow()`
* `Math.random()`
* A note on random. We use [a plugin](src/plugins/triangle-noise.js) to generate random.
* A note on random. We use [a plugin](src/plugins/math-random-uniformly-distributed.js) to generate random.
Random seeded _and_ generated, _both from the GPU_, is not as good as random from the CPU as there are more things that the CPU can seed random from.
However, we seed random on the GPU, _from a random value in the CPU_.
We then seed the subsequent randoms from the previous random value.
So we seed from CPU, and generate from GPU.
Which is still not as good as CPU, but closer.
While this isn't perfect, it should suffice in most scenarios.
In any case, we must give thanks to [RandomPower](https://www.randompower.eu/), and this [issue](https://github.com/gpujs/gpu.js/issues/498), for assisting in improving our implementation of random.
* `Math.round()`
* `Math.sign()`
* `Math.sin()`
Expand Down
53 changes: 15 additions & 38 deletions dist/gpu-browser-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* GPU Accelerated JavaScript
*
* @version 2.1.0
* @date Sun Oct 13 2019 15:49:17 GMT-0400 (Eastern Daylight Time)
* @date Fri Oct 18 2019 07:18:16 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -1842,7 +1842,6 @@ class CPUKernel extends Kernel {
module.exports = {
CPUKernel
};

},{"../../utils":111,"../function-builder":8,"../kernel":34,"./function-node":5,"./kernel-string":6}],8:[function(require,module,exports){
class FunctionBuilder {
static fromKernel(kernel, FunctionNode, extraNodeOptions) {
Expand Down Expand Up @@ -5087,7 +5086,6 @@ module.exports = {
GLKernel,
renderStrategy
};

},{"../../utils":111,"../kernel":34,"./texture/array-2-float":15,"./texture/array-2-float-2d":13,"./texture/array-2-float-3d":14,"./texture/array-3-float":18,"./texture/array-3-float-2d":16,"./texture/array-3-float-3d":17,"./texture/array-4-float":21,"./texture/array-4-float-2d":19,"./texture/array-4-float-3d":20,"./texture/float":24,"./texture/float-2d":22,"./texture/float-3d":23,"./texture/graphical":25,"./texture/memory-optimized":28,"./texture/memory-optimized-2d":26,"./texture/memory-optimized-3d":27,"./texture/unsigned":31,"./texture/unsigned-2d":29,"./texture/unsigned-3d":30}],13:[function(require,module,exports){
const { utils } = require('../../../utils');
const { GLTextureFloat } = require('./float');
Expand Down Expand Up @@ -5601,7 +5599,6 @@ class HeadlessGLKernel extends WebGLKernel {
module.exports = {
HeadlessGLKernel
};

},{"../gl/kernel-string":11,"../web-gl/kernel":67,"gl":1}],33:[function(require,module,exports){
class KernelValue {
constructor(value, settings) {
Expand Down Expand Up @@ -6128,7 +6125,6 @@ class Kernel {
module.exports = {
Kernel
};

},{"../input":107,"../utils":111}],35:[function(require,module,exports){
const fragmentShader = `__HEADER__;
__FLOAT_TACTIC_DECLARATION__;
Expand Down Expand Up @@ -7906,7 +7902,6 @@ const operatorMap = {
module.exports = {
WebGLFunctionNode
};

},{"../function-node":9}],37:[function(require,module,exports){
const { WebGLKernelValueBoolean } = require('./kernel-value/boolean');
const { WebGLKernelValueFloat } = require('./kernel-value/float');
Expand Down Expand Up @@ -9223,7 +9218,7 @@ const { GLKernel } = require('../gl/kernel');
const { FunctionBuilder } = require('../function-builder');
const { WebGLFunctionNode } = require('./function-node');
const { utils } = require('../../utils');
const triangleNoise = require('../../plugins/triangle-noise');
const mrud = require('../../plugins/math-random-uniformly-distributed');
const { fragmentShader } = require('./fragment-shader');
const { vertexShader } = require('./vertex-shader');
const { glKernelString } = require('../gl/kernel-string');
Expand All @@ -9235,7 +9230,7 @@ let testContext = null;
let testExtensions = null;
let features = null;

const plugins = [triangleNoise];
const plugins = [mrud];
const canvases = [];
const maxTexSizes = {};

Expand Down Expand Up @@ -10607,8 +10602,7 @@ class WebGLKernel extends GLKernel {
module.exports = {
WebGLKernel
};

},{"../../plugins/triangle-noise":109,"../../utils":111,"../function-builder":8,"../gl/kernel":12,"../gl/kernel-string":11,"./fragment-shader":35,"./function-node":36,"./kernel-value-maps":37,"./vertex-shader":68}],68:[function(require,module,exports){
},{"../../plugins/math-random-uniformly-distributed":109,"../../utils":111,"../function-builder":8,"../gl/kernel":12,"../gl/kernel-string":11,"./fragment-shader":35,"./function-node":36,"./kernel-value-maps":37,"./vertex-shader":68}],68:[function(require,module,exports){
const vertexShader = `__FLOAT_TACTIC_DECLARATION__;
__INT_TACTIC_DECLARATION__;
__SAMPLER_2D_TACTIC_DECLARATION__;
Expand Down Expand Up @@ -12862,7 +12856,6 @@ module.exports = {
kernelOrder,
kernelTypes
};

},{"./backend/cpu/kernel":7,"./backend/headless-gl/kernel":32,"./backend/web-gl/kernel":67,"./backend/web-gl2/kernel":102,"./kernel-run-shortcut":108,"./utils":111,"gpu-mock.js":3}],106:[function(require,module,exports){
const { GPU } = require('./gpu');
const { alias } = require('./alias');
Expand Down Expand Up @@ -13059,44 +13052,28 @@ module.exports = {
};
},{"./utils":111}],109:[function(require,module,exports){
const source = `

uniform highp float triangle_noise_seed;
highp float triangle_noise_shift = 0.000001;

//https://www.shadertoy.com/view/4t2SDh
// https://www.shadertoy.com/view/4t2SDh
//note: uniformly distributed, normalized rand, [0;1[
float nrand( vec2 n )
{
return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);
}
//note: remaps v to [0;1] in interval [a;b]
float remap( float a, float b, float v )
{
return clamp( (v-a) / (b-a), 0.0, 1.0 );
}

float n4rand( vec2 n )
{
float t = fract( triangle_noise_seed + triangle_noise_shift );
float nrnd0 = nrand( n + 0.07*t );
float nrnd1 = nrand( n + 0.11*t );
float nrnd2 = nrand( n + 0.13*t );
float nrnd3 = nrand( n + 0.17*t );
float result = (nrnd0+nrnd1+nrnd2+nrnd3) / 4.0;
triangle_noise_shift = result + 0.000001;
mediump float random_seed_shift = 0.00001;
uniform mediump float random_seed1;
uniform mediump float random_seed2;
float nrand(vec2 n) {
float result = fract(sin(dot(n.xy * vec2(random_seed1, random_seed_shift * random_seed2), vec2(12.9898, 78.233)))* 43758.5453);
random_seed_shift = result;
return result;
}`;

const name = 'triangle-noise-noise';
const name = 'math-random-uniformly-distributed';

const functionMatch = 'Math.random()';

const functionReplace = 'n4rand(vTexCoord)';
const functionReplace = 'nrand(vTexCoord)';

const functionReturnType = 'Number';

const onBeforeRun = (kernel) => {
kernel.setUniform1f('triangle_noise_seed', Math.random());
kernel.setUniform1f('random_seed1', Math.random());
kernel.setUniform1f('random_seed2', Math.random());
};

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

53 changes: 15 additions & 38 deletions dist/gpu-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* GPU Accelerated JavaScript
*
* @version 2.1.0
* @date Sun Oct 13 2019 15:49:17 GMT-0400 (Eastern Daylight Time)
* @date Fri Oct 18 2019 07:18:16 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -6606,7 +6606,6 @@ class CPUKernel extends Kernel {
module.exports = {
CPUKernel
};

},{"../../utils":112,"../function-builder":9,"../kernel":35,"./function-node":6,"./kernel-string":7}],9:[function(require,module,exports){
class FunctionBuilder {
static fromKernel(kernel, FunctionNode, extraNodeOptions) {
Expand Down Expand Up @@ -9851,7 +9850,6 @@ module.exports = {
GLKernel,
renderStrategy
};

},{"../../utils":112,"../kernel":35,"./texture/array-2-float":16,"./texture/array-2-float-2d":14,"./texture/array-2-float-3d":15,"./texture/array-3-float":19,"./texture/array-3-float-2d":17,"./texture/array-3-float-3d":18,"./texture/array-4-float":22,"./texture/array-4-float-2d":20,"./texture/array-4-float-3d":21,"./texture/float":25,"./texture/float-2d":23,"./texture/float-3d":24,"./texture/graphical":26,"./texture/memory-optimized":29,"./texture/memory-optimized-2d":27,"./texture/memory-optimized-3d":28,"./texture/unsigned":32,"./texture/unsigned-2d":30,"./texture/unsigned-3d":31}],14:[function(require,module,exports){
const { utils } = require('../../../utils');
const { GLTextureFloat } = require('./float');
Expand Down Expand Up @@ -10365,7 +10363,6 @@ class HeadlessGLKernel extends WebGLKernel {
module.exports = {
HeadlessGLKernel
};

},{"../gl/kernel-string":12,"../web-gl/kernel":68,"gl":2}],34:[function(require,module,exports){
class KernelValue {
constructor(value, settings) {
Expand Down Expand Up @@ -10892,7 +10889,6 @@ class Kernel {
module.exports = {
Kernel
};

},{"../input":108,"../utils":112}],36:[function(require,module,exports){
const fragmentShader = `__HEADER__;
__FLOAT_TACTIC_DECLARATION__;
Expand Down Expand Up @@ -12670,7 +12666,6 @@ const operatorMap = {
module.exports = {
WebGLFunctionNode
};

},{"../function-node":10}],38:[function(require,module,exports){
const { WebGLKernelValueBoolean } = require('./kernel-value/boolean');
const { WebGLKernelValueFloat } = require('./kernel-value/float');
Expand Down Expand Up @@ -13987,7 +13982,7 @@ const { GLKernel } = require('../gl/kernel');
const { FunctionBuilder } = require('../function-builder');
const { WebGLFunctionNode } = require('./function-node');
const { utils } = require('../../utils');
const triangleNoise = require('../../plugins/triangle-noise');
const mrud = require('../../plugins/math-random-uniformly-distributed');
const { fragmentShader } = require('./fragment-shader');
const { vertexShader } = require('./vertex-shader');
const { glKernelString } = require('../gl/kernel-string');
Expand All @@ -13999,7 +13994,7 @@ let testContext = null;
let testExtensions = null;
let features = null;

const plugins = [triangleNoise];
const plugins = [mrud];
const canvases = [];
const maxTexSizes = {};

Expand Down Expand Up @@ -15371,8 +15366,7 @@ class WebGLKernel extends GLKernel {
module.exports = {
WebGLKernel
};

},{"../../plugins/triangle-noise":110,"../../utils":112,"../function-builder":9,"../gl/kernel":13,"../gl/kernel-string":12,"./fragment-shader":36,"./function-node":37,"./kernel-value-maps":38,"./vertex-shader":69}],69:[function(require,module,exports){
},{"../../plugins/math-random-uniformly-distributed":110,"../../utils":112,"../function-builder":9,"../gl/kernel":13,"../gl/kernel-string":12,"./fragment-shader":36,"./function-node":37,"./kernel-value-maps":38,"./vertex-shader":69}],69:[function(require,module,exports){
const vertexShader = `__FLOAT_TACTIC_DECLARATION__;
__INT_TACTIC_DECLARATION__;
__SAMPLER_2D_TACTIC_DECLARATION__;
Expand Down Expand Up @@ -17626,7 +17620,6 @@ module.exports = {
kernelOrder,
kernelTypes
};

},{"./backend/cpu/kernel":8,"./backend/headless-gl/kernel":33,"./backend/web-gl/kernel":68,"./backend/web-gl2/kernel":103,"./kernel-run-shortcut":109,"./utils":112,"gpu-mock.js":4}],107:[function(require,module,exports){
const { GPU } = require('./gpu');
const { alias } = require('./alias');
Expand Down Expand Up @@ -17823,44 +17816,28 @@ module.exports = {
};
},{"./utils":112}],110:[function(require,module,exports){
const source = `

uniform highp float triangle_noise_seed;
highp float triangle_noise_shift = 0.000001;

//https://www.shadertoy.com/view/4t2SDh
// https://www.shadertoy.com/view/4t2SDh
//note: uniformly distributed, normalized rand, [0;1[
float nrand( vec2 n )
{
return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);
}
//note: remaps v to [0;1] in interval [a;b]
float remap( float a, float b, float v )
{
return clamp( (v-a) / (b-a), 0.0, 1.0 );
}

float n4rand( vec2 n )
{
float t = fract( triangle_noise_seed + triangle_noise_shift );
float nrnd0 = nrand( n + 0.07*t );
float nrnd1 = nrand( n + 0.11*t );
float nrnd2 = nrand( n + 0.13*t );
float nrnd3 = nrand( n + 0.17*t );
float result = (nrnd0+nrnd1+nrnd2+nrnd3) / 4.0;
triangle_noise_shift = result + 0.000001;
mediump float random_seed_shift = 0.00001;
uniform mediump float random_seed1;
uniform mediump float random_seed2;
float nrand(vec2 n) {
float result = fract(sin(dot(n.xy * vec2(random_seed1, random_seed_shift * random_seed2), vec2(12.9898, 78.233)))* 43758.5453);
random_seed_shift = result;
return result;
}`;

const name = 'triangle-noise-noise';
const name = 'math-random-uniformly-distributed';

const functionMatch = 'Math.random()';

const functionReplace = 'n4rand(vTexCoord)';
const functionReplace = 'nrand(vTexCoord)';

const functionReturnType = 'Number';

const onBeforeRun = (kernel) => {
kernel.setUniform1f('triangle_noise_seed', Math.random());
kernel.setUniform1f('random_seed1', Math.random());
kernel.setUniform1f('random_seed2', Math.random());
};

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions dist/gpu-browser.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/backend/web-gl/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { GLKernel } = require('../gl/kernel');
const { FunctionBuilder } = require('../function-builder');
const { WebGLFunctionNode } = require('./function-node');
const { utils } = require('../../utils');
const triangleNoise = require('../../plugins/triangle-noise');
const mrud = require('../../plugins/math-random-uniformly-distributed');
const { fragmentShader } = require('./fragment-shader');
const { vertexShader } = require('./vertex-shader');
const { glKernelString } = require('../gl/kernel-string');
Expand All @@ -14,7 +14,7 @@ let testContext = null;
let testExtensions = null;
let features = null;

const plugins = [triangleNoise];
const plugins = [mrud];
const canvases = [];
const maxTexSizes = {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const source = `

uniform highp float triangle_noise_seed;
highp float triangle_noise_shift = 0.000001;

Expand All @@ -19,19 +18,19 @@ float n4rand( vec2 n )
{
float t = fract( triangle_noise_seed + triangle_noise_shift );
float nrnd0 = nrand( n + 0.07*t );
float nrnd1 = nrand( n + 0.11*t );
float nrnd1 = nrand( n + 0.11*t );
float nrnd2 = nrand( n + 0.13*t );
float nrnd3 = nrand( n + 0.17*t );
float result = (nrnd0+nrnd1+nrnd2+nrnd3) / 4.0;
triangle_noise_shift = result + 0.000001;
return result;
}`;

const name = 'triangle-noise-noise';
const name = 'math-random-triangle-noise-noise';

const functionMatch = 'Math.random()';

const functionReplace = 'n4rand(vTexCoord)';
const functionReplace = 'nrand(vTexCoord)';

const functionReturnType = 'Number';

Expand Down
37 changes: 37 additions & 0 deletions src/plugins/math-random-uniformly-distributed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const source = `
// https://www.shadertoy.com/view/4t2SDh
//note: uniformly distributed, normalized rand, [0;1[
mediump float random_seed_shift = 0.00001;
uniform mediump float random_seed1;
uniform mediump float random_seed2;
float nrand(vec2 n) {
float result = fract(sin(dot(n.xy * vec2(random_seed1, random_seed_shift * random_seed2), vec2(12.9898, 78.233)))* 43758.5453);
random_seed_shift = result;
return result;
}`;

const name = 'math-random-uniformly-distributed';

const functionMatch = 'Math.random()';

const functionReplace = 'nrand(vTexCoord)';

const functionReturnType = 'Number';

const onBeforeRun = (kernel) => {
kernel.setUniform1f('random_seed1', Math.random());
kernel.setUniform1f('random_seed2', Math.random());
};

/**
*
* @type IPlugin
*/
module.exports = {
name,
onBeforeRun,
functionMatch,
functionReplace,
functionReturnType,
source
};
2 changes: 1 addition & 1 deletion test/internal/math.random.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function mathRandomUnique(mode) {
const kernel = gpu.createKernel(`function() {
${checkSource.join('\n')}
return 0;
}`, { output: [1] });
}`, { output: [1], debug: true });

const result = kernel();
assert.ok(result.every(value => value === 0));
Expand Down