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

Commit 8414c65

Browse files
committed
Adds automatic precision qualifier
1 parent 9502946 commit 8414c65

12 files changed

+28
-33
lines changed

src/render/shaders/ShaderBackground.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ ShaderBackground.vertex = [
2020
].join('\n');
2121

2222
ShaderBackground.fragment = [
23-
'precision mediump float;',
2423
'uniform sampler2D uTexture0;',
2524
'varying vec2 vTexCoord;',
2625
ShaderBase.strings.colorSpaceGLSL,

src/render/shaders/ShaderBase.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,35 @@ ShaderBase.strings.fragColorFunction = [
6262
'}'
6363
].join('\n');
6464

65-
var moveExtension = function (str) {
65+
var processShader = function (str) {
6666
// move extension enable/require to the top of file
67+
var extensions = '';
6768
var matches = str.match(/^\s*(#extension).*/gm);
68-
if (!matches) return str;
69-
var extMap = {};
70-
var exts = '';
71-
72-
for (var i = 0, nb = matches.length; i < nb; ++i) {
73-
var ext = matches[i].substr(matches[i].indexOf('#extension'));
74-
str = str.replace(matches[i], '');
75-
if (extMap[ext])
76-
continue;
77-
extMap[ext] = true;
78-
exts += ext + '\n';
69+
if (matches) {
70+
var extMap = {};
71+
72+
for (var i = 0, nb = matches.length; i < nb; ++i) {
73+
var ext = matches[i].substr(matches[i].indexOf('#extension'));
74+
str = str.replace(matches[i], '');
75+
if (extMap[ext])
76+
continue;
77+
extMap[ext] = true;
78+
extensions += ext + '\n';
79+
}
7980
}
80-
str = exts + str;
81+
82+
var version = '';
83+
if (str.indexOf('#version') === -1) {
84+
version += '#version 100\n';
85+
}
86+
87+
var precision = '';
88+
var regPrecision = /precision\s+(high|low|medium)p\s+float/;
89+
if (!regPrecision.test(str)) {
90+
precision += '#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n';
91+
}
92+
93+
str = version + extensions + precision + str;
8194
return str;
8295
};
8396

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

91104
var vShader = gl.createShader(gl.VERTEX_SHADER);
92-
gl.shaderSource(vShader, moveExtension(this.vertex + vname));
105+
gl.shaderSource(vShader, processShader(this.vertex + vname));
93106
gl.compileShader(vShader);
94107

95108
var fShader = gl.createShader(gl.FRAGMENT_SHADER);
96-
gl.shaderSource(fShader, moveExtension(this.fragment + fname));
109+
gl.shaderSource(fShader, processShader(this.fragment + fname));
97110
gl.compileShader(fShader);
98111

99112
var program = this.program = gl.createProgram();

src/render/shaders/ShaderContour.js

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ ShaderContour.vertex = [
2323

2424
ShaderContour.fragment = [
2525
'#extension GL_OES_standard_derivatives : enable',
26-
'precision mediump float;',
2726
'uniform sampler2D uTexture0;',
2827
'uniform vec4 uColor;',
2928
'varying vec2 vTexCoord;',

src/render/shaders/ShaderFlat.js

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ ShaderFlat.uniformNames = ['uColor'];
1414
Array.prototype.push.apply(ShaderFlat.uniformNames, ShaderBase.uniformNames.commonUniforms);
1515

1616
ShaderFlat.vertex = [
17-
'precision mediump float;',
1817
'attribute vec3 aVertex;',
1918
'attribute vec3 aMaterial;',
2019
ShaderBase.strings.vertUniforms,
@@ -26,7 +25,6 @@ ShaderFlat.vertex = [
2625
].join('\n');
2726

2827
ShaderFlat.fragment = [
29-
'precision mediump float;',
3028
'uniform vec3 uColor;',
3129
'void main() {',
3230
' gl_FragColor = vec4(uColor, 1.0);',

src/render/shaders/ShaderFxaa.js

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ ShaderFxaa.attributes = {};
1414
ShaderFxaa.uniformNames = ['uTexture0', 'uInvSize'];
1515

1616
ShaderFxaa.vertex = [
17-
'precision mediump float;',
1817
'attribute vec2 aVertex;',
1918
'uniform vec2 uInvSize;',
2019
'varying vec2 vUVNW;',
@@ -33,7 +32,6 @@ ShaderFxaa.vertex = [
3332
].join('\n');
3433

3534
ShaderFxaa.fragment = [
36-
'precision mediump float;',
3735
'uniform sampler2D uTexture0;',
3836
'uniform vec2 uInvSize;',
3937
'varying vec2 vUVNW;',

src/render/shaders/ShaderMatcap.js

-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ ShaderMatcap.uniformNames = ['uTexture0', 'uAlbedo'];
5252
Array.prototype.push.apply(ShaderMatcap.uniformNames, ShaderBase.uniformNames.commonUniforms);
5353

5454
ShaderMatcap.vertex = [
55-
'precision mediump float;',
5655
'attribute vec3 aVertex;',
5756
'attribute vec3 aNormal;',
5857
'attribute vec3 aColor;',
@@ -83,7 +82,6 @@ ShaderMatcap.vertex = [
8382
].join('\n');
8483

8584
ShaderMatcap.fragment = [
86-
'precision mediump float;',
8785
'uniform sampler2D uTexture0;',
8886
'varying vec3 vVertex;',
8987
'varying vec3 vVertexPres;',

src/render/shaders/ShaderMerge.js

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ ShaderMerge.attributes = {};
1313
ShaderMerge.uniformNames = ['uTexture0', 'uTexture1', 'uFilmic'];
1414

1515
ShaderMerge.vertex = [
16-
'precision mediump float;',
1716
'attribute vec2 aVertex;',
1817
'varying vec2 vTexCoord;',
1918
'void main() {',
@@ -23,7 +22,6 @@ ShaderMerge.vertex = [
2322
].join('\n');
2423

2524
ShaderMerge.fragment = [
26-
'precision mediump float;',
2725
'uniform sampler2D uTexture0;',
2826
'uniform sampler2D uTexture1;',
2927
'uniform int uFilmic;',

src/render/shaders/ShaderNormal.js

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ ShaderNormal.uniformNames = [];
1515
Array.prototype.push.apply(ShaderNormal.uniformNames, ShaderBase.uniformNames.commonUniforms);
1616

1717
ShaderNormal.vertex = [
18-
'precision mediump float;',
1918
'attribute vec3 aVertex;',
2019
'attribute vec3 aNormal;',
2120
'attribute vec3 aMaterial;',
@@ -35,7 +34,6 @@ ShaderNormal.vertex = [
3534
].join('\n');
3635

3736
ShaderNormal.fragment = [
38-
'precision mediump float;',
3937
'varying vec3 vVertex;',
4038
'varying vec3 vNormal;',
4139
'uniform float uAlpha;',

src/render/shaders/ShaderPBR.js

-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ ShaderPBR.uniformNames = ['uIblTransform', 'uTexture0', 'uAlbedo', 'uRoughness',
4343
Array.prototype.push.apply(ShaderPBR.uniformNames, ShaderBase.uniformNames.commonUniforms);
4444

4545
ShaderPBR.vertex = [
46-
'precision mediump float;',
4746
'attribute vec3 aVertex;',
4847
'attribute vec3 aNormal;',
4948
'attribute vec3 aColor;',
@@ -73,7 +72,6 @@ ShaderPBR.vertex = [
7372
].join('\n');
7473

7574
ShaderPBR.fragment = [
76-
'precision mediump float;',
7775
'varying vec3 vVertex;',
7876
'varying vec3 vNormal;',
7977
'varying vec3 vAlbedo;',

src/render/shaders/ShaderSelection.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ ShaderSelection.vertex = [
2020
].join('\n');
2121

2222
ShaderSelection.fragment = [
23-
'precision mediump float;',
2423
'uniform vec3 uColor;',
2524
'void main() {',
2625
' gl_FragColor = vec4(uColor, 1.0);',

src/render/shaders/ShaderUV.js

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ ShaderUV.uniformNames = ['uTexture0', 'uAlbedo'];
1313
Array.prototype.push.apply(ShaderUV.uniformNames, ShaderBase.uniformNames.commonUniforms);
1414

1515
ShaderUV.vertex = [
16-
'precision mediump float;',
1716
'attribute vec3 aVertex;',
1817
'attribute vec3 aNormal;',
1918
'attribute vec3 aColor;',
@@ -40,7 +39,6 @@ ShaderUV.vertex = [
4039
].join('\n');
4140

4241
ShaderUV.fragment = [
43-
'precision mediump float;',
4442
'uniform sampler2D uTexture0;',
4543
'varying vec3 vVertex;',
4644
'varying vec3 vNormal;',

src/render/shaders/ShaderWireframe.js

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ ShaderWireframe.vertex = [
2626
].join('\n');
2727

2828
ShaderWireframe.fragment = [
29-
'precision mediump float;',
3029
'void main() {',
3130
' gl_FragColor = vec4(0.0, 0.0, 0.0, 0.4);',
3231
'}'

0 commit comments

Comments
 (0)