From 2e7d7a2c8646ca93df738bc6c0df385bc686bf2f Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Wed, 4 Apr 2012 15:37:01 -0400 Subject: 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. --- src/library_gl.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') 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); }, -- cgit v1.2.3-70-g09d2