Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
save
  • Loading branch information
dsmilkov committed Sep 12, 2019
commit a3ec09ebc203a7031854f433a0e3e4fcfef6ab40
69 changes: 31 additions & 38 deletions tfjs-core/src/backends/webgl/flags_webgl_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import {ENV} from '../../environment';
import * as tf from '../../index';
import {describeWithFlags} from '../../jasmine_util';
import {webgl_util} from '../../webgl';

import {WEBGL_ENVS} from './backend_webgl_test_registry';
// import * as canvas_util from './canvas_util';
import * as webgl_context_manager from './webgl_context_manager';

describe('WEBGL_FORCE_F16_TEXTURES', () => {
afterAll(() => ENV.reset());
Expand Down Expand Up @@ -50,15 +49,11 @@ const RENDER_FLOAT16_ENVS = {
};

describeWithFlags('WEBGL_RENDER_FLOAT32_CAPABLE', RENDER_FLOAT32_ENVS, () => {
beforeEach(() => {
ENV.reset();
});

afterAll(() => ENV.reset());

it('should be independent of forcing f16 rendering', () => {
tf.webgl.forceHalfFloat();
expect(ENV.getBool('WEBGL_RENDER_FLOAT32_CAPABLE')).toBe(true);
// Undo the forcing of half float.
delete ENV.getFlags()['WEBGL_FORCE_F16_TEXTURES'];
});

it('if user is not forcing f16, device should render to f32', () => {
Expand Down Expand Up @@ -256,22 +251,20 @@ describe('WEBGL_CONV_IM2COL', () => {

describe('WEBGL_MAX_TEXTURE_SIZE', () => {
beforeEach(() => {
ENV.reset();
webgl_util.resetMaxTextureSize();

// TODO(kreeger): Fix this.
//spyOn(canvas_util, 'getWebGLContext').and.returnValue({
// MAX_TEXTURE_SIZE: 101,
// getParameter: (param: number) => {
// if (param === 101) {
// return 50;
// }
// throw new Error(`Got undefined param ${param}.`);
// }
//});
spyOn(webgl_context_manager, 'getContextByVersion').and.returnValue({
MAX_TEXTURE_SIZE: 101,
getParameter: (param: number) => {
if (param === 101) {
return 50;
}
throw new Error(`Got undefined param ${param}.`);
}
});
});

afterAll(() => {
ENV.reset();
webgl_util.resetMaxTextureSize();
});

Expand All @@ -281,36 +274,36 @@ describe('WEBGL_MAX_TEXTURE_SIZE', () => {
});

describe('WEBGL_MAX_TEXTURES_IN_SHADER', () => {
// let maxTextures: number;
let maxTextures: number;
beforeEach(() => {
ENV.reset();
webgl_util.resetMaxTexturesInShader();

// TODO(kreeger): Fix this.
// spyOn(canvas_util, 'getWebGLContext').and.callFake(() => {
// return {
// MAX_TEXTURE_IMAGE_UNITS: 101,
// getParameter: (param: number) => {
// if (param === 101) {
// return maxTextures;
// }
// throw new Error(`Got undefined param ${param}.`);
// }
// };
// });
// Forge the max textures flag between different tests.
delete ENV.getFlags()['WEBGL_MAX_TEXTURES_IN_SHADER'];

spyOn(webgl_context_manager, 'getContextByVersion').and.callFake(() => {
return {
MAX_TEXTURE_IMAGE_UNITS: 101,
getParameter: (param: number) => {
if (param === 101) {
return maxTextures;
}
throw new Error(`Got undefined param ${param}.`);
}
};
});
});

afterAll(() => {
ENV.reset();
webgl_util.resetMaxTexturesInShader();
});

it('is a function of gl.getParameter(MAX_TEXTURE_IMAGE_UNITS)', () => {
// maxTextures = 10;
maxTextures = 10;
expect(ENV.getNumber('WEBGL_MAX_TEXTURES_IN_SHADER')).toBe(10);
});

it('is capped at 16', () => {
// maxTextures = 20;
maxTextures = 20;
expect(ENV.getNumber('WEBGL_MAX_TEXTURES_IN_SHADER')).toBe(16);
});
});
Expand Down
55 changes: 24 additions & 31 deletions tfjs-core/src/backends/webgl/gpgpu_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import * as util from '../../util';

import * as gpgpu_util from './gpgpu_util';
import * as tex_util from './tex_util';
import {getActiveContext} from './webgl_context_manager';
import {TextureConfig} from './tex_util';
import {callAndCheck, checkWebGLError} from './webgl_check';
import {getActiveContext} from './webgl_context_manager';
import {WebGL1DisjointQueryTimerExtension, WebGL2DisjointQueryTimerExtension} from './webgl_types';
import * as webgl_util from './webgl_util';

Expand Down Expand Up @@ -51,8 +52,8 @@ export class GPGPUContext {
const gl = getActiveContext();
// WebGL 2.0 enables texture floats without an extension.
if (ENV.getNumber('WEBGL_VERSION') === 1) {
this.textureFloatExtension = webgl_util.getExtensionOrThrow(
gl, this.debug, 'OES_texture_float');
this.textureFloatExtension =
webgl_util.getExtensionOrThrow(gl, this.debug, 'OES_texture_float');
this.colorBufferFloatExtension =
gl.getExtension('WEBGL_color_buffer_float');

Expand All @@ -64,8 +65,7 @@ export class GPGPUContext {
const COLOR_BUFFER_FLOAT = 'EXT_color_buffer_float';
const COLOR_BUFFER_HALF_FLOAT = 'EXT_color_buffer_half_float';
if (webgl_util.hasExtension(gl, COLOR_BUFFER_FLOAT)) {
this.colorBufferFloatExtension =
gl.getExtension(COLOR_BUFFER_FLOAT);
this.colorBufferFloatExtension = gl.getExtension(COLOR_BUFFER_FLOAT);
} else if (webgl_util.hasExtension(gl, COLOR_BUFFER_HALF_FLOAT)) {
this.colorBufferHalfFloatExtension =
gl.getExtension(COLOR_BUFFER_HALF_FLOAT);
Expand Down Expand Up @@ -105,19 +105,15 @@ export class GPGPUContext {
}
const debug = true;
const gl = getActiveContext();
webgl_util.checkWebGLError(gl);
webgl_util.callAndCheck(gl, debug, () => gl.finish());
checkWebGLError(gl);
callAndCheck(gl, debug, () => gl.finish());
// TODO(kreeger): This bind framebuffer call can throw an INVALID_OPERATION
// error on WebGL2 - fix this.
webgl_util.callAndCheck(
gl, debug, () => gl.bindFramebuffer(gl.FRAMEBUFFER, null));
webgl_util.callAndCheck(
gl, debug, () => gl.deleteFramebuffer(this.framebuffer));
webgl_util.callAndCheck(
gl, debug, () => gl.bindBuffer(gl.ARRAY_BUFFER, null));
webgl_util.callAndCheck(
gl, debug, () => gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null));
webgl_util.callAndCheck(gl, debug, () => gl.deleteBuffer(this.indexBuffer));
callAndCheck(gl, debug, () => gl.bindFramebuffer(gl.FRAMEBUFFER, null));
callAndCheck(gl, debug, () => gl.deleteFramebuffer(this.framebuffer));
callAndCheck(gl, debug, () => gl.bindBuffer(gl.ARRAY_BUFFER, null));
callAndCheck(gl, debug, () => gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null));
callAndCheck(gl, debug, () => gl.deleteBuffer(this.indexBuffer));
this.disposed = true;
// TODO(kreeger): Needed or used?
// disposeWebGLContext();
Expand Down Expand Up @@ -181,8 +177,8 @@ export class GPGPUContext {
getActiveContext(), this.debug, this.framebuffer);
this.outputTexture = null;
}
console.log(' texture: ' + texture);
webgl_util.callAndCheck(
// console.log(' texture: ' + texture);
callAndCheck(
getActiveContext(), true,
() => getActiveContext().deleteTexture(texture));
}
Expand Down Expand Up @@ -280,9 +276,8 @@ export class GPGPUContext {
gl,
this.debug,
);
webgl_util.callAndCheck(
gl, this.debug, () => gl.attachShader(program, vertexShader));
webgl_util.callAndCheck(
callAndCheck(gl, this.debug, () => gl.attachShader(program, vertexShader));
callAndCheck(
gl, this.debug, () => gl.attachShader(program, fragmentShader));
webgl_util.linkProgram(gl, this.debug, program);
if (this.debug) {
Expand All @@ -302,7 +297,7 @@ export class GPGPUContext {
this.program = null;
}
if (program != null) {
webgl_util.callAndCheck(
callAndCheck(
getActiveContext(), this.debug,
() => getActiveContext().deleteProgram(program));
}
Expand All @@ -314,7 +309,7 @@ export class GPGPUContext {
if ((this.program != null) && this.debug) {
webgl_util.validateProgram(getActiveContext(), this.debug, this.program);
}
webgl_util.callAndCheck(
callAndCheck(
getActiveContext(), this.debug,
() => getActiveContext().useProgram(program));
}
Expand All @@ -335,7 +330,7 @@ export class GPGPUContext {
public getAttributeLocation(program: WebGLProgram, attribute: string):
number {
this.throwIfDisposed();
return webgl_util.callAndCheck(
return callAndCheck(
getActiveContext(), this.debug,
() => getActiveContext().getAttribLocation(program, attribute));
}
Expand Down Expand Up @@ -396,14 +391,14 @@ export class GPGPUContext {
if (this.debug) {
this.debugValidate();
}
webgl_util.callAndCheck(
callAndCheck(
gl, this.debug,
() => gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0));
}

public blockUntilAllProgramsCompleted() {
this.throwIfDisposed();
webgl_util.callAndCheck(
callAndCheck(
getActiveContext(), this.debug, () => getActiveContext().finish());
}

Expand Down Expand Up @@ -595,16 +590,14 @@ export class GPGPUContext {
webgl_util.validateFramebuffer(gl);
}
this.outputTexture = outputMatrixTextureMaybePacked;
webgl_util.callAndCheck(
gl, this.debug, () => gl.viewport(0, 0, width, height));
webgl_util.callAndCheck(
gl, this.debug, () => gl.scissor(0, 0, width, height));
callAndCheck(gl, this.debug, () => gl.viewport(0, 0, width, height));
callAndCheck(gl, this.debug, () => gl.scissor(0, 0, width, height));
}

private setOutputMatrixWriteRegionDriver(
x: number, y: number, width: number, height: number) {
this.throwIfDisposed();
webgl_util.callAndCheck(
callAndCheck(
getActiveContext(), this.debug,
() => getActiveContext().scissor(x, y, width, height));
}
Expand Down
Loading