@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
305 lines • 14.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var jasmine_util_1 = require("../../jasmine_util");
var test_util_1 = require("../../test_util");
var glsl_version_1 = require("./glsl_version");
var gpgpu_context_1 = require("./gpgpu_context");
var tex_util = require("./tex_util");
var DOWNLOAD_FLOAT_ENVS = {
'WEBGL_DOWNLOAD_FLOAT_ENABLED': true
};
jasmine_util_1.describeWithFlags('GPGPUContext downloadMatrixFromTexture', DOWNLOAD_FLOAT_ENVS, function () {
var gpgpu;
var texture;
beforeEach(function () {
gpgpu = new gpgpu_context_1.GPGPUContext();
gpgpu.enableAutomaticDebugValidation(true);
texture = gpgpu.createFloat32MatrixTexture(1, 1);
});
afterEach(function () {
gpgpu.deleteMatrixTexture(texture);
gpgpu.dispose();
});
it('returns 1x1 matrix that was uploaded', function () {
gpgpu.uploadMatrixToTexture(texture, 1, 1, new Float32Array([1.234]));
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
test_util_1.expectNumbersClose(result[0], 1.234);
});
it('returns 2x2 matrix that was uploaded', function () {
var texture2 = gpgpu.createFloat32MatrixTexture(2, 2);
gpgpu.uploadMatrixToTexture(texture2, 2, 2, new Float32Array([1.234, 2, 3, 4]));
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(texture2, 2, 2);
test_util_1.expectArraysClose(result, new Float32Array([1.234, 2, 3, 4]));
gpgpu.deleteMatrixTexture(texture2);
});
it('uses texture parameter', function () {
var texture2 = gpgpu.createFloat32MatrixTexture(1, 1);
gpgpu.uploadMatrixToTexture(texture, 1, 1, new Float32Array([1]));
gpgpu.uploadMatrixToTexture(texture2, 1, 1, new Float32Array([2]));
var read1 = gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
var read2 = gpgpu.downloadFloat32MatrixFromOutputTexture(texture2, 1, 1);
test_util_1.expectNumbersClose(read1[0], 1);
test_util_1.expectNumbersClose(read2[0], 2);
gpgpu.deleteMatrixTexture(texture2);
});
});
jasmine_util_1.describeWithFlags('GPGPUContext color texture with float', DOWNLOAD_FLOAT_ENVS, function () {
var gpgpu;
var texture;
afterEach(function () {
gpgpu.deleteMatrixTexture(texture);
gpgpu.dispose();
});
it('basic', function () {
gpgpu = new gpgpu_context_1.GPGPUContext();
gpgpu.enableAutomaticDebugValidation(true);
texture = gpgpu.createFloat32MatrixTexture(1, 1);
gpgpu.setOutputMatrixTexture(texture, 1, 1);
gpgpu.gl.clearColor(0.123, 0, 0, 0);
gpgpu.gl.clear(gpgpu.gl.COLOR_BUFFER_BIT);
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
test_util_1.expectNumbersClose(result[0], 0.123);
});
});
jasmine_util_1.describeWithFlags('GPGPUContext setOutputMatrixTexture', DOWNLOAD_FLOAT_ENVS, function () {
var gpgpu;
var texture;
beforeEach(function () {
gpgpu = new gpgpu_context_1.GPGPUContext();
gpgpu.enableAutomaticDebugValidation(true);
texture = gpgpu.createFloat32MatrixTexture(1, 1);
});
afterEach(function () {
gpgpu.deleteMatrixTexture(texture);
gpgpu.dispose();
});
it('sets the output texture property to the output texture', function () {
gpgpu.setOutputMatrixTexture(texture, 1, 1);
expect(gpgpu.outputTexture).toBe(texture);
});
it('rebinds the output texture to the color buffer target', function () {
var output = gpgpu.createFloat32MatrixTexture(1, 1);
gpgpu.uploadMatrixToTexture(texture, 1, 1, new Float32Array([10]));
gpgpu.setOutputMatrixTexture(output, 1, 1);
var tBeforeClear = gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
test_util_1.expectNumbersClose(tBeforeClear[0], 10);
gpgpu.gl.clearColor(1, 0, 0, 0);
gpgpu.gl.clear(gpgpu.gl.COLOR_BUFFER_BIT);
var tAfterClear = gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
test_util_1.expectNumbersClose(tAfterClear[0], 10);
gpgpu.deleteMatrixTexture(output);
});
it('resets output texture to null if nothing was previously bound', function () {
expect(gpgpu.outputTexture).toBeNull();
gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
expect(gpgpu.outputTexture).toBeNull();
});
it('sets the gl viewport to the output texture dimensions', function () {
var columns = 456;
var rows = 123;
var output = gpgpu.createFloat32MatrixTexture(rows, columns);
gpgpu.setOutputMatrixTexture(output, rows, columns);
var expected = new Int32Array([0, 0, columns, rows]);
expect(gpgpu.gl.getParameter(gpgpu.gl.VIEWPORT)).toEqual(expected);
gpgpu.deleteMatrixTexture(output);
});
it('doesn\'t change gl viewport when downloading a non-output tex', function () {
var output = gpgpu.createFloat32MatrixTexture(128, 128);
gpgpu.setOutputMatrixTexture(output, 128, 128);
gpgpu.downloadFloat32MatrixFromOutputTexture(texture, 1, 1);
var expected = new Int32Array([0, 0, 128, 128]);
expect(gpgpu.gl.getParameter(gpgpu.gl.VIEWPORT)).toEqual(expected);
gpgpu.deleteMatrixTexture(output);
});
});
jasmine_util_1.describeWithFlags('GPGPUContext setOutputPackedMatrixTexture', DOWNLOAD_FLOAT_ENVS, function () {
var gpgpu;
var texture;
beforeEach(function () {
gpgpu = new gpgpu_context_1.GPGPUContext();
gpgpu.enableAutomaticDebugValidation(true);
});
afterEach(function () {
if (texture != null) {
gpgpu.deleteMatrixTexture(texture);
}
gpgpu.dispose();
});
it('sets the output texture property to the output texture', function () {
texture = gpgpu.createPackedMatrixTexture(1, 1);
gpgpu.setOutputPackedMatrixTexture(texture, 1, 1);
expect(gpgpu.outputTexture).toBe(texture);
});
it('sets the gl viewport to the output packed texture dimensions', function () {
var columns = 456;
var rows = 123;
texture = gpgpu.createPackedMatrixTexture(rows, columns);
gpgpu.setOutputPackedMatrixTexture(texture, rows, columns);
var _a = tex_util.getPackedMatrixTextureShapeWidthHeight(rows, columns), width = _a[0], height = _a[1];
var expected = new Int32Array([0, 0, width, height]);
expect(gpgpu.gl.getParameter(gpgpu.gl.VIEWPORT)).toEqual(expected);
});
});
jasmine_util_1.describeWithFlags('GPGPUContext setOutputMatrixWriteRegion', DOWNLOAD_FLOAT_ENVS, function () {
var gpgpu;
var program;
var output;
beforeEach(function () {
gpgpu = new gpgpu_context_1.GPGPUContext();
gpgpu.enableAutomaticDebugValidation(true);
var glsl = glsl_version_1.getGlslDifferences();
var src = glsl.version + "\n precision highp float;\n " + glsl.defineOutput + "\n void main() {\n " + glsl.output + " = vec4(2,0,0,0);\n }\n ";
program = gpgpu.createProgram(src);
output = gpgpu.createFloat32MatrixTexture(4, 4);
gpgpu.uploadMatrixToTexture(output, 4, 4, new Float32Array(16));
gpgpu.setOutputMatrixTexture(output, 4, 4);
gpgpu.setProgram(program);
});
afterEach(function () {
gpgpu.deleteMatrixTexture(output);
gpgpu.deleteProgram(program);
gpgpu.dispose();
});
it('writes to all pixels by default', function () {
gpgpu.executeProgram();
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(output, 4, 4);
var expected = new Float32Array(4 * 4);
expected.fill(2);
test_util_1.expectArraysClose(result, expected);
});
it('sets the scissor box to the requested parameters', function () {
gpgpu.setOutputMatrixWriteRegion(0, 1, 2, 3);
var scissorBox = gpgpu.gl.getParameter(gpgpu.gl.SCISSOR_BOX);
expect(scissorBox[0]).toEqual(2);
expect(scissorBox[1]).toEqual(0);
expect(scissorBox[2]).toEqual(3);
expect(scissorBox[3]).toEqual(1);
});
it('writes only to center 2x2 region of 4x4 texture', function () {
gpgpu.setOutputMatrixWriteRegion(1, 2, 1, 2);
gpgpu.executeProgram();
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(output, 4, 4);
var expected = new Float32Array([0, 0, 0, 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0]);
test_util_1.expectArraysClose(result, expected);
});
it('preserves data from previous writes outside of write region', function () {
gpgpu.setOutputMatrixWriteRegion(0, 1, 0, 4);
gpgpu.executeProgram();
gpgpu.setOutputMatrixWriteRegion(3, 1, 0, 4);
gpgpu.executeProgram();
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(output, 4, 4);
var expected = new Float32Array([2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2]);
test_util_1.expectArraysClose(result, expected);
});
it('writes adjacent cells across multiple calls', function () {
for (var row = 0; row < 4; ++row) {
for (var col = 0; col < 4; ++col) {
gpgpu.setOutputMatrixWriteRegion(row, 1, col, 1);
gpgpu.executeProgram();
}
}
var result = gpgpu.downloadFloat32MatrixFromOutputTexture(output, 4, 4);
var expected = new Float32Array(4 * 4);
expected.fill(2);
test_util_1.expectArraysClose(result, expected);
});
});
jasmine_util_1.describeWithFlags('GPGPUContext', DOWNLOAD_FLOAT_ENVS, function () {
var gpgpu;
beforeEach(function () {
gpgpu = new gpgpu_context_1.GPGPUContext();
gpgpu.enableAutomaticDebugValidation(true);
});
afterEach(function () {
gpgpu.dispose();
});
it('throws an error if used after dispose', function () {
var gpgpuContext = new gpgpu_context_1.GPGPUContext();
gpgpuContext.dispose();
expect(gpgpuContext.dispose).toThrowError();
});
it('throws an error if validation is on and framebuffer incomplete', function () {
var glsl = glsl_version_1.getGlslDifferences();
var src = glsl.version + "\n precision highp float;\n void main() {}\n ";
var program = gpgpu.createProgram(src);
var result = gpgpu.createFloat32MatrixTexture(1, 1);
gpgpu.setOutputMatrixTexture(result, 1, 1);
gpgpu.setProgram(program);
gpgpu.deleteMatrixTexture(result);
expect(gpgpu.executeProgram).toThrowError();
gpgpu.deleteProgram(program);
});
});
describe('gpgpu_context binSearchLastTrue', function () {
it('[false]', function () {
var a = [false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(-1);
});
it('[true]', function () {
var a = [true];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(0);
});
it('[false, false]', function () {
var a = [false, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(-1);
});
it('[true, false]', function () {
var a = [true, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(0);
});
it('[true, true]', function () {
var a = [true, true];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(1);
});
it('[false, false, false]', function () {
var a = [false, false, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(-1);
});
it('[true, false, false]', function () {
var a = [true, false, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(0);
});
it('[true, true, false]', function () {
var a = [true, true, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(1);
});
it('[true, true, true]', function () {
var a = [true, true, true];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(2);
});
it('[false, false, false, false]', function () {
var a = [false, false, false, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(-1);
});
it('[true, false, false, false]', function () {
var a = [true, false, false, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(0);
});
it('[true, true, false, false]', function () {
var a = [true, true, false, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(1);
});
it('[true, true, true, false]', function () {
var a = [true, true, true, false];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(2);
});
it('[true, true, true, true]', function () {
var a = [true, true, true, true];
var arr = a.map(function (x) { return function () { return x; }; });
expect(gpgpu_context_1.binSearchLastTrue(arr)).toBe(3);
});
});
//# sourceMappingURL=gpgpu_context_test.js.map