From cfab4e1c421ef674af4d7c7a99299554e3f413fa Mon Sep 17 00:00:00 2001 From: Nikhil Thorat Date: Mon, 9 Oct 2017 16:07:24 -0400 Subject: [PATCH] Use byte packing on safari desktop by checking whether the color buffer float extension exists. (#196) * ios * merge * Merge remote-tracking branch 'origin/master' into ios * ios * its working...? haha, not. * improve precision * fix accuracy on iOS * fix the faster / slightly less precise version * slight speedup * fix numerical issues on ios. (use resultUV instead of gl_FragCoord, and highp int) * merge master * actually merge * flag guard byte textures * Merge remote-tracking branch 'origin' into ios * Merge remote-tracking branch 'origin' into ios * more changes * merge * start pulling tests apart * tests * merge * get remaining tests to pass * Merge remote-tracking branch 'origin' into ios * remove comments, remove imagenet util change * imagenet * test_util commits * ndarray tests * test_util blank space * softmax underflow on mac, copy gpu test revert * revert _gpu_tests * remove console.log * fix lint errors * respond to comments * Updates to env for desktop safari * merge master * remove canvas width / height --- src/environment.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/environment.ts b/src/environment.ts index dd2fca97e3..df4dc85cbb 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -54,6 +54,7 @@ function getWebGLRenderingContext(webGLVersion: number): WebGLRenderingContext { } const tempCanvas = document.createElement('canvas'); + if (webGLVersion === 1) { return (tempCanvas.getContext('webgl') || tempCanvas.getContext('experimental-webgl')) as @@ -100,20 +101,33 @@ function isFloatTextureReadPixelsEnabled(webGLVersion: number): boolean { return false; } - if (webGLVersion === 2) { - // WebGL 2 has floating point textures enabled by default. - return true; - } - const gl = getWebGLRenderingContext(webGLVersion); - gl.getExtension('OES_texture_float'); - gl.getExtension('WEBGL_color_buffer_float'); + + let floatExtension; + let colorBufferFloatExtension; + if (webGLVersion === 1) { + floatExtension = gl.getExtension('OES_texture_float'); + colorBufferFloatExtension = gl.getExtension('WEBGL_color_buffer_float'); + if (floatExtension == null || colorBufferFloatExtension == null) { + return false; + } + } else { + colorBufferFloatExtension = gl.getExtension('EXT_color_buffer_float'); + if (colorBufferFloatExtension == null) { + return false; + } + } const frameBuffer = gl.createFramebuffer(); const texture = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture); - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.FLOAT, null); + + // tslint:disable-next-line:no-any + const internalFormat = webGLVersion === 2 ? (gl as any).RGBA32F : gl.RGBA; + gl.texImage2D( + gl.TEXTURE_2D, 0, internalFormat, 1, 1, 0, gl.RGBA, gl.FLOAT, null); + gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);