Skip to content

Commit

Permalink
Downgrading webGL precision if target hardware does not support highe…
Browse files Browse the repository at this point in the history
…st (fabricjs#4433)

* Downgrading webGL precision if target hardware does not support highest

* moved webGlPrecision to fabric property
  • Loading branch information
ozooner authored and asturur committed Oct 31, 2017
1 parent f052320 commit a7c5fc6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/filters/base_filter.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass(/** @lends fabric.Imag
if (!this.vertexSource || !this.fragmentSource) {
return;
}
if(fabric.webGlPrecision !== 'highp'){
fragmentSource = fragmentSource.replace(/precision highp float/g, 'precision ' + fabric.webGlPrecision + ' float');
}

var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexSource || this.vertexSource);
gl.compileShader(vertexShader);
Expand Down
25 changes: 25 additions & 0 deletions src/filters/webgl_backend.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

'use strict';


/**
* Tests if webgl supports certain precision
* @param {WebGL} Canvas WebGL context to test on
* @param {String} Precision to test can be any of following: 'lowp', 'mediump', 'highp'
* @returns {Boolean} Whether the user's browser WebGL supports given precision.
*/
function testPrecision(gl, precision){
var fragmentSource = 'precision ' + precision + ' float;\nvoid main(){}';
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentSource);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
return false;
}
return true;
}

/**
* Indicate whether this filtering backend is supported by the user's browser.
* @param {Number} tileSize check if the tileSize is supported
Expand All @@ -19,6 +37,13 @@
if (gl) {
fabric.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
isSupported = fabric.maxTextureSize >= tileSize;
var precisions = ['highp', 'mediump', 'lowp'];
for(var i = 0; i < 3; i++){
if(testPrecision(gl, precisions[i])){
fabric.webGlPrecision = precisions[i];
break;
};
}
}
this.isSupported = isSupported;
return isSupported;
Expand Down

0 comments on commit a7c5fc6

Please sign in to comment.