Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Adds automatic precision qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
stephomi committed May 20, 2017
1 parent 9502946 commit 8414c65
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/render/shaders/ShaderBackground.js
Expand Up @@ -20,7 +20,6 @@ ShaderBackground.vertex = [
].join('\n');

ShaderBackground.fragment = [
'precision mediump float;',
'uniform sampler2D uTexture0;',
'varying vec2 vTexCoord;',
ShaderBase.strings.colorSpaceGLSL,
Expand Down
43 changes: 28 additions & 15 deletions src/render/shaders/ShaderBase.js
Expand Up @@ -62,22 +62,35 @@ ShaderBase.strings.fragColorFunction = [
'}'
].join('\n');

var moveExtension = function (str) {
var processShader = function (str) {
// move extension enable/require to the top of file
var extensions = '';
var matches = str.match(/^\s*(#extension).*/gm);
if (!matches) return str;
var extMap = {};
var exts = '';

for (var i = 0, nb = matches.length; i < nb; ++i) {
var ext = matches[i].substr(matches[i].indexOf('#extension'));
str = str.replace(matches[i], '');
if (extMap[ext])
continue;
extMap[ext] = true;
exts += ext + '\n';
if (matches) {
var extMap = {};

for (var i = 0, nb = matches.length; i < nb; ++i) {
var ext = matches[i].substr(matches[i].indexOf('#extension'));
str = str.replace(matches[i], '');
if (extMap[ext])
continue;
extMap[ext] = true;
extensions += ext + '\n';
}
}
str = exts + str;

var version = '';
if (str.indexOf('#version') === -1) {
version += '#version 100\n';
}

var precision = '';
var regPrecision = /precision\s+(high|low|medium)p\s+float/;
if (!regPrecision.test(str)) {
precision += '#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n';
}

str = version + extensions + precision + str;
return str;
};

Expand All @@ -89,11 +102,11 @@ ShaderBase.getOrCreate = function (gl) {
var fname = '\n#define SHADER_NAME ' + this.fragmentName + '\n';

var vShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vShader, moveExtension(this.vertex + vname));
gl.shaderSource(vShader, processShader(this.vertex + vname));
gl.compileShader(vShader);

var fShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fShader, moveExtension(this.fragment + fname));
gl.shaderSource(fShader, processShader(this.fragment + fname));
gl.compileShader(fShader);

var program = this.program = gl.createProgram();
Expand Down
1 change: 0 additions & 1 deletion src/render/shaders/ShaderContour.js
Expand Up @@ -23,7 +23,6 @@ ShaderContour.vertex = [

ShaderContour.fragment = [
'#extension GL_OES_standard_derivatives : enable',
'precision mediump float;',
'uniform sampler2D uTexture0;',
'uniform vec4 uColor;',
'varying vec2 vTexCoord;',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderFlat.js
Expand Up @@ -14,7 +14,6 @@ ShaderFlat.uniformNames = ['uColor'];
Array.prototype.push.apply(ShaderFlat.uniformNames, ShaderBase.uniformNames.commonUniforms);

ShaderFlat.vertex = [
'precision mediump float;',
'attribute vec3 aVertex;',
'attribute vec3 aMaterial;',
ShaderBase.strings.vertUniforms,
Expand All @@ -26,7 +25,6 @@ ShaderFlat.vertex = [
].join('\n');

ShaderFlat.fragment = [
'precision mediump float;',
'uniform vec3 uColor;',
'void main() {',
' gl_FragColor = vec4(uColor, 1.0);',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderFxaa.js
Expand Up @@ -14,7 +14,6 @@ ShaderFxaa.attributes = {};
ShaderFxaa.uniformNames = ['uTexture0', 'uInvSize'];

ShaderFxaa.vertex = [
'precision mediump float;',
'attribute vec2 aVertex;',
'uniform vec2 uInvSize;',
'varying vec2 vUVNW;',
Expand All @@ -33,7 +32,6 @@ ShaderFxaa.vertex = [
].join('\n');

ShaderFxaa.fragment = [
'precision mediump float;',
'uniform sampler2D uTexture0;',
'uniform vec2 uInvSize;',
'varying vec2 vUVNW;',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderMatcap.js
Expand Up @@ -52,7 +52,6 @@ ShaderMatcap.uniformNames = ['uTexture0', 'uAlbedo'];
Array.prototype.push.apply(ShaderMatcap.uniformNames, ShaderBase.uniformNames.commonUniforms);

ShaderMatcap.vertex = [
'precision mediump float;',
'attribute vec3 aVertex;',
'attribute vec3 aNormal;',
'attribute vec3 aColor;',
Expand Down Expand Up @@ -83,7 +82,6 @@ ShaderMatcap.vertex = [
].join('\n');

ShaderMatcap.fragment = [
'precision mediump float;',
'uniform sampler2D uTexture0;',
'varying vec3 vVertex;',
'varying vec3 vVertexPres;',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderMerge.js
Expand Up @@ -13,7 +13,6 @@ ShaderMerge.attributes = {};
ShaderMerge.uniformNames = ['uTexture0', 'uTexture1', 'uFilmic'];

ShaderMerge.vertex = [
'precision mediump float;',
'attribute vec2 aVertex;',
'varying vec2 vTexCoord;',
'void main() {',
Expand All @@ -23,7 +22,6 @@ ShaderMerge.vertex = [
].join('\n');

ShaderMerge.fragment = [
'precision mediump float;',
'uniform sampler2D uTexture0;',
'uniform sampler2D uTexture1;',
'uniform int uFilmic;',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderNormal.js
Expand Up @@ -15,7 +15,6 @@ ShaderNormal.uniformNames = [];
Array.prototype.push.apply(ShaderNormal.uniformNames, ShaderBase.uniformNames.commonUniforms);

ShaderNormal.vertex = [
'precision mediump float;',
'attribute vec3 aVertex;',
'attribute vec3 aNormal;',
'attribute vec3 aMaterial;',
Expand All @@ -35,7 +34,6 @@ ShaderNormal.vertex = [
].join('\n');

ShaderNormal.fragment = [
'precision mediump float;',
'varying vec3 vVertex;',
'varying vec3 vNormal;',
'uniform float uAlpha;',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderPBR.js
Expand Up @@ -43,7 +43,6 @@ ShaderPBR.uniformNames = ['uIblTransform', 'uTexture0', 'uAlbedo', 'uRoughness',
Array.prototype.push.apply(ShaderPBR.uniformNames, ShaderBase.uniformNames.commonUniforms);

ShaderPBR.vertex = [
'precision mediump float;',
'attribute vec3 aVertex;',
'attribute vec3 aNormal;',
'attribute vec3 aColor;',
Expand Down Expand Up @@ -73,7 +72,6 @@ ShaderPBR.vertex = [
].join('\n');

ShaderPBR.fragment = [
'precision mediump float;',
'varying vec3 vVertex;',
'varying vec3 vNormal;',
'varying vec3 vAlbedo;',
Expand Down
1 change: 0 additions & 1 deletion src/render/shaders/ShaderSelection.js
Expand Up @@ -20,7 +20,6 @@ ShaderSelection.vertex = [
].join('\n');

ShaderSelection.fragment = [
'precision mediump float;',
'uniform vec3 uColor;',
'void main() {',
' gl_FragColor = vec4(uColor, 1.0);',
Expand Down
2 changes: 0 additions & 2 deletions src/render/shaders/ShaderUV.js
Expand Up @@ -13,7 +13,6 @@ ShaderUV.uniformNames = ['uTexture0', 'uAlbedo'];
Array.prototype.push.apply(ShaderUV.uniformNames, ShaderBase.uniformNames.commonUniforms);

ShaderUV.vertex = [
'precision mediump float;',
'attribute vec3 aVertex;',
'attribute vec3 aNormal;',
'attribute vec3 aColor;',
Expand All @@ -40,7 +39,6 @@ ShaderUV.vertex = [
].join('\n');

ShaderUV.fragment = [
'precision mediump float;',
'uniform sampler2D uTexture0;',
'varying vec3 vVertex;',
'varying vec3 vNormal;',
Expand Down
1 change: 0 additions & 1 deletion src/render/shaders/ShaderWireframe.js
Expand Up @@ -26,7 +26,6 @@ ShaderWireframe.vertex = [
].join('\n');

ShaderWireframe.fragment = [
'precision mediump float;',
'void main() {',
' gl_FragColor = vec4(0.0, 0.0, 0.0, 0.4);',
'}'
Expand Down

0 comments on commit 8414c65

Please sign in to comment.