Skip to content

Commit

Permalink
Merge bb52030 into ddd3eb0
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Jul 27, 2019
2 parents ddd3eb0 + bb52030 commit a92c789
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 63 deletions.
46 changes: 46 additions & 0 deletions modules/shadertools/test/modules/fp64/fp64-utils.spec.js
@@ -0,0 +1,46 @@
import test from 'tape-catch';
import {fp64} from '@luma.gl/shadertools';
import {_Pose as Pose} from 'math.gl';

test('fp64#fp64LowPart', t => {
const x = Math.PI;
const x32 = new Float32Array([x])[0];

t.is(fp64.fp64LowPart(x) + x32, x, 'returns correct result');

t.end();
});

test('fp64#fp64ify', t => {
const x = Math.PI;

const xHi = Math.fround(x);
const xLow = x - xHi;

t.deepEqual(fp64.fp64ify(x), [xHi, xLow], 'returns correct result');

const target = new Array(10);
fp64.fp64ify(x, target, 4);

t.deepEqual(target.slice(4, 6), [xHi, xLow], 'populates target array');

t.end();
});

test('fp64#fp64ifyMatrix4', t => {
const matrix = new Pose({
yaw: -0.00032679972032649654,
pitch: 0.0017499351314303354,
roll: 0.0006456183945756637,
x: -29.95600959142378,
y: 33.72513623131151,
z: -0.40325097780902214
}).getTransformationMatrix();

const matrix64 = fp64.fp64ifyMatrix4(matrix);

t.ok(matrix64 instanceof Float32Array, 'returns Float32Array');
t.ok(matrix64.every(Number.isFinite), 'returns valid matrix');

t.end();
});
1 change: 1 addition & 0 deletions modules/shadertools/test/modules/index.js
@@ -1,4 +1,5 @@
import './fp64/fp64-arithmetic-transform.spec';
import './fp64/fp64-utils.spec';
import './lights/lights.spec';
import './picking/picking.spec';
import './phong-lighting/phong-lighting.spec';
Expand Down
14 changes: 0 additions & 14 deletions modules/webgl-state-tracker/src/globals.js

This file was deleted.

Expand Up @@ -4,25 +4,7 @@
import GL_STATE_SETTERS from './webgl-function-to-parameters-table';
import {GL_PARAMETER_DEFAULTS} from '../unified-parameter-api/webgl-parameter-tables';
import {setParameters, getParameters} from '../unified-parameter-api/unified-parameter-api';
import {assert} from '../utils';

export const clone = x => {
return Array.isArray(x) || ArrayBuffer.isView(x) ? x.slice() : x;
};

export const deepEqual = (x, y) => {
const isArrayX = Array.isArray(x) || ArrayBuffer.isView(x);
const isArrayY = Array.isArray(y) || ArrayBuffer.isView(y);
if (isArrayX && isArrayY && x.length === y.length) {
for (let i = 0; i < x.length; ++i) {
if (x[i] !== y[i]) {
return false;
}
}
return true;
}
return x === y;
};
import {assert, deepArrayEqual} from '../utils';

// HELPER FUNCTIONS - INSTALL GET/SET INTERCEPTORS (SPYS) ON THE CONTEXT

Expand Down Expand Up @@ -136,7 +118,7 @@ class GLState {
for (const key in values) {
assert(key !== undefined);
// Check that value hasn't already been shadowed
if (!deepEqual(values[key], this.cache[key])) {
if (!deepArrayEqual(values[key], this.cache[key])) {
valueChanged = true;
oldValue = this.cache[key];

Expand Down
Expand Up @@ -115,7 +115,7 @@ export function resetParameters(gl) {

// Get all parameters that have been modified from a pure context state
export function getModifiedParameters(gl) {
const values = getParameters(GL_PARAMETER_DEFAULTS);
const values = getParameters(gl, Object.keys(GL_PARAMETER_DEFAULTS));
const modified = {};
for (const key in GL_PARAMETER_DEFAULTS) {
if (!deepArrayEqual(values[key], GL_PARAMETER_DEFAULTS[key])) {
Expand Down
Expand Up @@ -129,17 +129,24 @@ test('WebGLState#setParameters (Argument expansion for ***SeperateFunc setters))
test('WebGLState#withParameters', t => {
const {gl} = fixture;

const checkParameters = expected => {
for (const key in expected) {
const value = getParameter(gl, key);
t.deepEqual(value, expected[key], `got expected value ${stringifyTypedArray(value)}`);
}
};

resetParameters(gl);

// Initialize parameters
setParameters(gl, {
clearColor: [0, 0, 0, 0],
[GL.BLEND]: false
});

let clearColor = getParameter(gl, GL.COLOR_CLEAR_VALUE);
let blendState = getParameter(gl, GL.BLEND);
t.deepEqual(clearColor, [0, 0, 0, 0], `got expected value ${stringifyTypedArray(clearColor)}`);
t.deepEqual(blendState, false, `got expected value ${stringifyTypedArray(blendState)}`);
checkParameters({
[GL.COLOR_CLEAR_VALUE]: [0, 0, 0, 0],
[GL.BLEND]: false
});

withParameters(
gl,
Expand All @@ -148,21 +155,46 @@ test('WebGLState#withParameters', t => {
[GL.BLEND]: true
},
() => {
clearColor = getParameter(gl, GL.COLOR_CLEAR_VALUE);
blendState = getParameter(gl, GL.BLEND);
t.deepEqual(
clearColor,
[0, 1, 0, 1],
`got expected value ${stringifyTypedArray(clearColor)}`
);
t.deepEqual(blendState, true, `got expected value ${stringifyTypedArray(blendState)}`);
// Parameters should be updated
checkParameters({
[GL.COLOR_CLEAR_VALUE]: [0, 1, 0, 1],
[GL.BLEND]: true
});
}
);

clearColor = getParameter(gl, GL.COLOR_CLEAR_VALUE);
blendState = getParameter(gl, GL.BLEND);
t.deepEqual(clearColor, [0, 0, 0, 0], `got expected value ${stringifyTypedArray(clearColor)}`);
t.deepEqual(blendState, false, `got expected value ${stringifyTypedArray(blendState)}`);
// Parameters should be restored
checkParameters({
[GL.COLOR_CLEAR_VALUE]: [0, 0, 0, 0],
[GL.BLEND]: false
});

t.throws(
() =>
withParameters(
gl,
{
clearColor: [0, 1, 0, 1],
[GL.BLEND]: true,
nocatch: false
},
() => {
// Parameters should be updated
checkParameters({
[GL.COLOR_CLEAR_VALUE]: [0, 1, 0, 1],
[GL.BLEND]: true
});
throw new Error();
}
),
'Operation throws error'
);

// Parameters should be restored
checkParameters({
[GL.COLOR_CLEAR_VALUE]: [0, 0, 0, 0],
[GL.BLEND]: false
});

t.end();
});
Expand Down
Expand Up @@ -6,7 +6,8 @@ import trackContextState, {
popContextState,
getParameter,
setParameters,
resetParameters
resetParameters,
getModifiedParameters
} from '@luma.gl/webgl-state-tracker';

import {
Expand Down Expand Up @@ -162,3 +163,74 @@ test('WebGLState#gl API', t => {

t.end();
});

test('WebGLState#intercept gl calls', t => {
const {gl} = fixture;

resetParameters(gl);

pushContextState(gl);

gl.blendEquation(gl.FUNC_SUBTRACT, gl.FUNC_SUBTRACT);
t.is(getParameter(gl, gl.BLEND_EQUATION_RGB), gl.FUNC_SUBTRACT, 'direct gl call is tracked');

gl.blendFunc(gl.ONE, gl.ONE);
t.is(getParameter(gl, gl.BLEND_SRC_RGB), gl.ONE, 'direct gl call is tracked');

gl.stencilMask(8);
t.is(getParameter(gl, gl.STENCIL_WRITEMASK), 8, 'direct gl call is tracked');

gl.stencilFunc(gl.NEVER, 0, 1);
t.is(getParameter(gl, gl.STENCIL_FUNC), gl.NEVER, 'direct gl call is tracked');

gl.stencilOp(gl.KEEP, gl.ZERO, gl.REPLACE);
t.is(getParameter(gl, gl.STENCIL_PASS_DEPTH_FAIL), gl.ZERO, 'direct gl call is tracked');

popContextState(gl);

// Verify default values.
for (const key in GL_PARAMETER_DEFAULTS) {
const value = getParameter(gl, key);
t.deepEqual(
value,
GL_PARAMETER_DEFAULTS[key],
`got expected value ${stringifyTypedArray(value)}`
);
}

t.end();
});

test('WebGLState#getModifiedParameters', t => {
const {gl} = fixture;

resetParameters(gl);

let modifiedParameters = getModifiedParameters(gl);

t.deepEqual(modifiedParameters, {}, 'Nothing is changed');

pushContextState(gl);

// Set custom values and verify
setParameters(gl, ENUM_STYLE_SETTINGS_SET1);
modifiedParameters = getModifiedParameters(gl);

for (const key in ENUM_STYLE_SETTINGS_SET1) {
t.deepEqual(
modifiedParameters[key],
ENUM_STYLE_SETTINGS_SET1[key],
`got expected value ${stringifyTypedArray(modifiedParameters[key])} for key: ${key}`
);
}

for (const key in modifiedParameters) {
if (!(key in ENUM_STYLE_SETTINGS_SET1)) {
t.fail('should not contain keys that are not changed');
}
}

popContextState(gl);

t.end();
});
@@ -1,5 +1,6 @@
import test from 'tape-catch';
import {
setParameter,
setParameters,
getParameter,
getParameters,
Expand Down Expand Up @@ -28,17 +29,24 @@ function stringifyTypedArray(v) {

test('WebGL#set and get', t => {
const {gl} = fixture;
const values = {
[GL.CULL_FACE]: true
};
resetParameters(gl);

let value = getParameter(gl, GL.CULL_FACE);
t.deepEqual(value, false, `got expected value ${stringifyTypedArray(value)}`);
setParameters(gl, values, {});

setParameter(gl, GL.CULL_FACE, true);
value = getParameter(gl, GL.CULL_FACE);
t.deepEqual(value, true, `got expected value ${stringifyTypedArray(value)}`);

value = getParameter(gl, GL.DEPTH_CLEAR_VALUE);
t.is(value, 1, `got expected value ${stringifyTypedArray(value)}`);

setParameter(gl, GL.DEPTH_CLEAR_VALUE, -1);
value = getParameter(gl, GL.DEPTH_CLEAR_VALUE);
t.is(value, -1, `got expected value ${stringifyTypedArray(value)}`);

t.throws(() => setParameter(gl, GL.NON_EXIST, 0), 'throws on bad constant');

t.end();
});

Expand Down
19 changes: 13 additions & 6 deletions modules/webgl-state-tracker/test/utils/deep-array-equal.spec.js
Expand Up @@ -2,14 +2,21 @@ import test from 'tape-catch';
import {deepArrayEqual} from '@luma.gl/webgl-state-tracker/utils';

test('WebGLState#deepArrayEqual', t => {
// TODO - what happened with our test cases??
const ARRAY = [0, 1, 2];

const TEST_CASES = [
{title: 'null', x: null, y: null, result: true},
{title: 'number', x: null, y: null, result: true},
{title: 'shallow-equal array 1', x: null, y: null, result: true},
{title: 'deep-equal array', x: null, y: null, result: true},
{title: 'deep-equal array/typed array', x: null, y: null, result: true},
{title: 'different arrays', x: null, y: null, result: true}
{title: 'number', x: 1, y: 1, result: true},
{title: 'number not equal', x: 1, y: 2, result: false},
{title: 'shallow-equal array 1', x: ARRAY, y: ARRAY, result: true},
{title: 'deep-equal array', x: ARRAY, y: [0, 1, 2], result: true},
{
title: 'deep-equal array/typed array',
x: new Float32Array(ARRAY),
y: new Float32Array(ARRAY),
result: true
},
{title: 'different arrays', x: [0, 1], y: [0, 2], result: false}
];

for (const tc of TEST_CASES) {
Expand Down

0 comments on commit a92c789

Please sign in to comment.