aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEhsan Akhgari <ehsan.akhgari@gmail.com>2012-04-04 15:37:01 -0400
committerEhsan Akhgari <ehsan.akhgari@gmail.com>2012-04-04 15:37:01 -0400
commit2e7d7a2c8646ca93df738bc6c0df385bc686bf2f (patch)
tree1fedd9cb0077dfcaee50df0d858243ca204e116e /src
parentc677d9e446160698dae8bd0de030a4e7262597cc (diff)
Enable the standard derivatives extension if needed
We need to find the functions defined by this extension for fragment shaders, and enable it if the shader uses them. Note that this extension is automatically enabled in GL ES 2.0 but not in WebGL.
Diffstat (limited to 'src')
-rw-r--r--src/library_gl.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/library_gl.js b/src/library_gl.js
index d99d59ec..1bb94253 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -24,6 +24,31 @@ var LibraryGL = {
}
return 0;
},
+
+ // Find a token in a shader source string
+ findToken: function(source, token) {
+ function isIdentChar(ch) {
+ if (ch >= 48 && ch <= 57) // 0-9
+ return true;
+ if (ch >= 65 && ch <= 90) // A-Z
+ return true;
+ if (ch >= 97 && ch <= 122) // a-z
+ return true;
+ return false;
+ }
+ var i = source.indexOf(token);
+ if (i < 0) {
+ return false;
+ }
+ if (i > 0 && isIdentChar(source[i - 1])) {
+ return false;
+ }
+ i += token.length;
+ if (i < source.length - 1 && isIdentChar(source[i + 1])) {
+ return false;
+ }
+ return true;
+ },
},
glGetString: function(name_) {
@@ -529,6 +554,21 @@ var LibraryGL = {
}
source += frag;
}
+ // Let's see if we need to enable the standard derivatives extension
+ type = Module.ctx.getShaderParameter(GL.shaders[shader], 0x8B4F /* GL_SHADER_TYPE */);
+ if (type == 0x8B30 /* GL_FRAGMENT_SHADER */) {
+ if (GL.findToken(source, "dFdx") ||
+ GL.findToken(source, "dFdy") ||
+ GL.findToken(source, "fwidth")) {
+ source = "#extension GL_OES_standard_derivatives : enable\n" + source;
+ var extension = Module.ctx.getExtension("OES_standard_derivatives");
+#if GL_DEBUG
+ if (!extension) {
+ Module.printErr("Shader attempts to use the standard derivatives extension which is not available.");
+ }
+#endif
+ }
+ }
Module.ctx.shaderSource(GL.shaders[shader], source);
},