Skip to content

Commit

Permalink
Patch gl.getExtension() so we can provide an additional manual check …
Browse files Browse the repository at this point in the history
…if an extension is supported, due to Firefox incorrectly reporting EXT_shader_texture_lod status. Fixes google#183
  • Loading branch information
jsantell committed Nov 30, 2018
1 parent 52ebfee commit 6918b7c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/three-components/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {$tick} from '../model-viewer-base.js';

import TextureUtils from './TextureUtils.js';
import {ARRenderer} from './ARRenderer.js';
import * as WebGLExtensionUtils from './WebGLExtensionUtils.js';

const GAMMA_FACTOR = 2.2;
const DPR = window.devicePixelRatio;
Expand Down Expand Up @@ -50,6 +51,8 @@ export default class Renderer extends EventDispatcher {

this.canvas = document.createElement('canvas');
this.context = this.canvas.getContext('webgl', webGlOptions);
WebGLExtensionUtils.applyExtensionCompatibility(this.context);

this.renderer =
new WebGLRenderer({canvas: this.canvas, context: this.context});
this.renderer.autoClear = false;
Expand Down
62 changes: 62 additions & 0 deletions src/three-components/WebGLExtensionUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const testShaders = {
// In some Firefox builds (mobile Android on Pixel at least),
// EXT_shader_texture_lod is reported as being supported, but
// fails in practice.
// @see https://bugzilla.mozilla.org/show_bug.cgi?id=1451287
'EXT_shader_texture_lod': `
#extension GL_EXT_shader_texture_lod : enable
precision mediump float;
uniform sampler2D tex;
void main() {
gl_FragColor = texture2DLodEXT(tex, vec2(0.0, 0.0), 0.0);
}
`,
};

const confirmExtension = (gl, name) => {
const shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(shader, testShaders[name]);
gl.compileShader(shader);

const status = gl.getShaderParameter(shader, gl.COMPILE_STATUS);

gl.deleteShader(shader);
return status;
};

/**
* Patch the values reported by WebGLRenderingContext's
* extension store to fix compatibility issues.
*/
export const applyExtensionCompatibility = gl => {
const getExtension = gl.getExtension;
gl.getExtension = name => {
let extension;

if (testShaders[name]) {
extension = getExtension.call(gl, name);
if (extension && !confirmExtension(gl, name)) {
extension = null;
}
} else {
extension = getExtension.call(gl, name);
}

return extension;
};
};

0 comments on commit 6918b7c

Please sign in to comment.