From 13f2b9433f428fbc7c508765efede30c00eeba2b Mon Sep 17 00:00:00 2001 From: Pavel Dobryakov Date: Fri, 1 Dec 2017 12:46:04 +0300 Subject: [PATCH] improve checking rendering to texture support --- script.js | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/script.js b/script.js index 0640988..adc9c23 100644 --- a/script.js +++ b/script.js @@ -40,10 +40,24 @@ function getWebGLContext (canvas) { gl.clearColor(0.0, 0.0, 0.0, 1.0); - const internalFormat = isWebGL2 ? gl.RGBA16F : gl.RGBA; - const internalFormatRG = isWebGL2 ? gl.RG16F : gl.RGBA; - const formatRG = isWebGL2 ? gl.RG : gl.RGBA; const halfFloatTexType = isWebGL2 ? gl.HALF_FLOAT : halfFloat.HALF_FLOAT_OES; + const internalFormat = isWebGL2 ? gl.RGBA16F : gl.RGBA; + let internalFormatRG = isWebGL2 ? gl.RG16F : gl.RGBA; + let formatRG = isWebGL2 ? gl.RG : gl.RGBA; + + if (isWebGL2) { + if (!supportRenderTextureFormat(gl, internalFormatRG, formatRG, halfFloatTexType)) { + internalFormatRG = gl.RGBA16F; + formatRG = gl.RGBA; + + ga('send', 'event', 'webgl2', 'rg not supported'); + if (!supportRenderTextureFormat(gl, internalFormatRG, formatRG, halfFloatTexType)) + ga('send', 'event', 'webgl2', 'rgba not supported'); + } + } else { + if (!supportRenderTextureFormat(gl, internalFormatRG, formatRG, halfFloatTexType)) + ga('send', 'event', 'webgl', 'rgba not supported'); + } return { gl, @@ -57,6 +71,25 @@ function getWebGLContext (canvas) { }; } +function supportRenderTextureFormat (gl, internalFormat, format, type) { + let texture = gl.createTexture(); + gl.bindTexture(gl.TEXTURE_2D, texture); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); + gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, 4, 4, 0, format, type, null); + + let fbo = gl.createFramebuffer(); + gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); + gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); + + const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER); + if (status != gl.FRAMEBUFFER_COMPLETE) + return false; + return true; +} + function startGUI () { var gui = new dat.GUI({ width: 300 }); gui.add(config, 'TEXTURE_DOWNSAMPLE', { Full: 0, Half: 1, Quarter: 2 }).name('resolution').onFinishChange(initFramebuffers); @@ -438,14 +471,6 @@ function createFBO (texId, w, h, internalFormat, format, type, param) { gl.viewport(0, 0, w, h); gl.clear(gl.COLOR_BUFFER_BIT); - const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER); - if (status != gl.FRAMEBUFFER_COMPLETE) { - ga('send', 'pageview', { - 'dimension1': status, - 'dimension2': isWebGL2 - }); - } - return [texture, fbo, texId]; }