From d109a22298d50eee954728cfd7dba24f0b85819d Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 14:08:42 -0400 Subject: [PATCH 01/57] Initial commit of GLSLModernizer and other WebGL2 changes --- Source/Renderer/Context.js | 31 ++- Source/Renderer/GLSLModernizer.js | 351 ++++++++++++++++++++++++++++ Source/Renderer/ShaderCache.js | 11 +- Source/Renderer/ShaderSource.js | 40 +++- Source/Renderer/Texture.js | 14 ++ Source/Shaders/GlobeFS.glsl | 4 +- Specs/Renderer/ShaderProgramSpec.js | 4 +- Specs/Renderer/ShaderSourceSpec.js | 10 +- 8 files changed, 440 insertions(+), 25 deletions(-) create mode 100644 Source/Renderer/GLSLModernizer.js diff --git a/Source/Renderer/Context.js b/Source/Renderer/Context.js index 7eff7dc3fa52..e8af2d025cf2 100644 --- a/Source/Renderer/Context.js +++ b/Source/Renderer/Context.js @@ -268,13 +268,17 @@ define([ this._antialias = gl.getContextAttributes().antialias; // Query and initialize extensions - this._standardDerivatives = !!getExtension(gl, ['OES_standard_derivatives']); - this._elementIndexUint = !!getExtension(gl, ['OES_element_index_uint']); - this._depthTexture = !!getExtension(gl, ['WEBGL_depth_texture', 'WEBKIT_WEBGL_depth_texture']); - this._textureFloat = !!getExtension(gl, ['OES_texture_float']); - this._fragDepth = !!getExtension(gl, ['EXT_frag_depth']); + this._standardDerivatives = !!getExtension(gl, ['OES_standard_derivatives']) || this._webgl2; + this._elementIndexUint = !!getExtension(gl, ['OES_element_index_uint']) || this._webgl2; + this._depthTexture = !!getExtension(gl, ['WEBGL_depth_texture', 'WEBKIT_WEBGL_depth_texture']) || this._webgl2; + this._textureFloat = !!getExtension(gl, ['OES_texture_float']) || this._webgl2; + this._fragDepth = !!getExtension(gl, ['EXT_frag_depth']) || this._webgl2; this._debugShaders = getExtension(gl, ['WEBGL_debug_shaders']); + if (this._webgl2) { + this._colorBufferFloat = !!getExtension(gl, ['EXT_color_buffer_float']); + } + this._s3tc = !!getExtension(gl, ['WEBGL_compressed_texture_s3tc', 'MOZ_WEBGL_compressed_texture_s3tc', 'WEBKIT_WEBGL_compressed_texture_s3tc']); this._pvrtc = !!getExtension(gl, ['WEBGL_compressed_texture_pvrtc', 'WEBKIT_WEBGL_compressed_texture_pvrtc']); this._etc1 = !!getExtension(gl, ['WEBGL_compressed_texture_etc1']); @@ -614,6 +618,20 @@ define([ } }, + /** + * true if the EXT_color_buffer_float extension is supported. This + * extension makes the formats gl.R16F, gl.RG16F, gl.RGBA16F, gl.R32F, gl.RG32F, + * gl.RGBA32F, gl.R11F_G11F_B10F color renderable. + * @memberof Context.prototype + * @type {Boolean} + * @see {@link https://www.khronos.org/registry/webgl/extensions/EXT_color_buffer_float/} + */ + colorBufferFloat : { + get : function() { + return this._colorBufferFloat; + } + }, + /** * true if the WEBGL_draw_buffers extension is supported. This * extensions provides support for multiple render targets. The framebuffer object can have mutiple @@ -1076,7 +1094,8 @@ define([ }), uniformMap : overrides.uniformMap, owner : overrides.owner, - framebuffer : overrides.framebuffer + framebuffer : overrides.framebuffer, + pass : overrides.pass }); }; diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js new file mode 100644 index 000000000000..95c373455510 --- /dev/null +++ b/Source/Renderer/GLSLModernizer.js @@ -0,0 +1,351 @@ +/*global define*/ +define([ + '../Core/DeveloperError', + ], function( + DeveloperError) { + 'use strict'; + + /** + * A function to port GLSL shaders from GLSL ES 1.00 to GLSL ES 2.00 + * + * This function is nowhere near comprehensive or complete. It just + * handles some common cases. + * + * @private + */ + function glslModernizeShaderSource(shaderSource, isFragmentShader) { + for (var i = 0; i < shaderSource.sources.length; i++) { + shaderSource.sources[i] = + glslModernizeShaderText(shaderSource.sources[i], isFragmentShader, i === 0); + } + shaderSource.defines.push("MODERNIZED"); + } + + // Note that this function requires the presence of the + // "#define OUTPUT_DECLARATION" line that is appended + // by ShaderSource + function glslModernizeShaderText(source, isFragmentShader, first) { + var mainFunctionRegex = /void\s+main\s*\((void)?\)/; + var outputDeclarationRegex = /#define OUTPUT_DECLARATION/; + var splitSource = source.split('\n'); + + if (source.search(/#version 300 es/g) !== -1) { + return source; + } + + var outputDeclarationLine = -1; + var number; + for (number = 0; number < splitSource.length; number++) { + var line = splitSource[number]; + if (outputDeclarationRegex.exec(line)) { + outputDeclarationLine = number; + break; + } + }; + + if (outputDeclarationLine == -1) { + for (number = 0; number < splitSource.length; number++) { + var line = splitSource[number]; + if (mainFunctionRegex.exec(line)) { + outputDeclarationLine = number; + } + }; + } + + if (outputDeclarationLine == -1) { + throw new DeveloperError('Could not find a ' + + '#define OUTPUT_DECLARATION ' + + 'nor a main function!'); + } + + function safeNameFalseNegative(regex, region) { + var regExpStr = regex.toString(); + regExpStr = regExpStr.match(/\/([^\/]+)(\/.)?/)[1]; + var somethingBadInString = + new RegExp("[a-zA-Z0-9_]+" + regExpStr, 'g'); + var searchResult = region.search(somethingBadInString); + if (searchResult === -1) { + return true; + } else { + return false; + } + } + + function safeNameFind(regex, str) { + var originalMatch = regex.exec(str); + if (originalMatch == null) { + return -1; + } + var endPos = originalMatch.index + originalMatch[0].length; + var region = str.substring(0, endPos); + + var possiblyFalseNegative = safeNameFalseNegative(regex, region); + if (possiblyFalseNegative) { + return endPos; + } else { + var childResult = safeNameFind(regex, str.substring(endPos)); + if (childResult == -1) { + return -1; + } else { + return endPos + childResult; + } + } + } + + function safeNameReplace(regex, str, replacement) { + var originalMatch = regex.exec(str); + if (originalMatch == null) { + return str; + } + var endPos = originalMatch.index + originalMatch[0].length; + var region = str.substring(0, endPos); + + var possiblyFalseNegative = safeNameFalseNegative(regex, region); + if (possiblyFalseNegative) { + return region.replace(regex, replacement) + + safeNameReplace(regex, str.substr(endPos), replacement); + } else { + return region + + safeNameReplace(regex, str.substr(endPos), replacement); + } + } + + function replaceInSource(regex, replacement) { + for (var number = 0; number < splitSource.length; number++) { + splitSource[number] = safeNameReplace(regex, splitSource[number], replacement); + } + } + + function findInSource(regex) { + for (var number = 0; number < splitSource.length; number++) { + var line = splitSource[number]; + if (safeNameFind(regex, line) !== -1) { + return true; + } + } + return false; + } + + function compileSource() { + var wholeSource = ""; + for (var number = 0; number < splitSource.length; number++) { + wholeSource += splitSource[number] + "\n"; + } + return wholeSource; + } + + function setAdd(variable, set1) { + if (set1.indexOf(variable) === -1) + set1.push(variable); + } + + function setUnion(set1, set2) { + for (var a = 0; a < set2.length; a++) { + setAdd(set2[a], set1); + } + } + + function getVariablePreprocessorBranch(variablesThatWeCareAbout) { + var variableMap = {}; + var negativeMap = {}; + + for (var a = 0; a < variablesThatWeCareAbout.length; a++) { + var variableThatWeCareAbout = variablesThatWeCareAbout[a]; + variableMap[variableThatWeCareAbout] = [null]; + } + + var stack = []; + for (var i = 0; i < splitSource.length; i++) { + var line = splitSource[i]; + var hasIF = line.search(/(#ifdef|#if)/g) !== -1; + var hasELSE = line.search(/#else/g) !== -1; + var hasENDIF = line.search(/#endif/g) !== -1; + + if (hasIF) { + stack.push(line); + } else if (hasELSE) { + var top = stack[stack.length - 1]; + var op = top.replace("ifdef", "ifndef"); + if (op.search(/if/g) !== -1) { + op = op.replace(/(#if\s+)(\S*)([^]*)/, "$1!($2)$3"); + } + stack.pop(); + stack.push(op); + + negativeMap[top] = op; + negativeMap[op] = top; + } else if (hasENDIF) { + stack.pop(); + } else if (line.search(/layout/g) === -1) { + for (a = 0; a < variablesThatWeCareAbout.length; a++) { + var care = variablesThatWeCareAbout[a]; + if (line.indexOf(care) !== -1) { + if (variableMap[care].length === 1 && + variableMap[care][0] === null) { + variableMap[care] = stack.slice(); + } else { + variableMap[care] = variableMap[care].filter( + function(x) { return stack.indexOf(x) >= 0; }); + } + } + } + } + } + + // This code is probably no longer needed... it was used to handle + // removing negative conditions + for (var care in variableMap) { + if (variableMap.hasOwnProperty(care)) { + var entry = variableMap[care]; + var toDelete = []; + for (var b = 0; b < entry.length; b++) { + var item = entry[b]; + if (entry.indexOf(negativeMap[item]) !== -1) { + toDelete.push(item); + toDelete.push(negativeMap[item]); + } + } + + for (var c = 0; c < toDelete.length; c++) { + var ind = entry.indexOf(toDelete[c]); + if (ind !== -1) { + entry.splice(ind, 1); + } + } + } + } + + for (var care in variableMap) { + if (variableMap.hasOwnProperty(care)) { + if (variableMap.length === 1 && + variableMap[0] === null) { + variableMap.splice(0, 1); + } + } + } + + return variableMap; + } + + function removeExtension(name) { + var regex = "#extension\\s+GL_" + name + "\\s+:\\s+[a-zA-Z0-9]+\\s*$"; + replaceInSource(new RegExp(regex, "g"), ""); + } + + var variableSet = []; + + for (var i = 0; i < 10; i++) { + var fragDataString = "gl_FragData\\[" + i + "\\]"; + var newOutput = 'czm_out' + i; + var regex = new RegExp(fragDataString, "g"); + if (source.search(fragDataString) != -1) { + setAdd(newOutput, variableSet); + replaceInSource(regex, newOutput); + splitSource.splice(outputDeclarationLine, 0, + "layout(location = " + i + ") out vec4 " + + newOutput + ";"); + outputDeclarationLine += 1; + } + } + + var czmFragColor = "czm_fragColor"; + if (findInSource(/gl_FragColor/)) { + setAdd(czmFragColor, variableSet); + replaceInSource(/gl_FragColor/, czmFragColor); + splitSource.splice(outputDeclarationLine, 0, "layout(location = 0) out vec4 czm_fragColor;"); + outputDeclarationLine += 1; + } + + var variableMap = getVariablePreprocessorBranch(variableSet); + var lineAdds = {}; + for (var c = 0; c < splitSource.length; c++) { + var l = splitSource[c]; + for (var care in variableMap) { + if (variableMap.hasOwnProperty(care)) { + var matchVar = + new RegExp("(layout)[^]+(out)[^]+(" + care + ")[^]+", "g"); + if (matchVar.exec(l) !== null) { + lineAdds[l] = care; + } + } + } + } + + for (var layoutDeclaration in lineAdds) { + if (lineAdds.hasOwnProperty(layoutDeclaration)) { + var variableName = lineAdds[layoutDeclaration]; + var lineNumber = splitSource.indexOf(layoutDeclaration); + var entry = variableMap[variableName]; + var depth = entry.length; + var d; + for (d = 0; d < depth; d++) { + splitSource.splice(lineNumber, 0, entry[d]); + } + lineNumber += depth + 1; + for (d = depth - 1; d >= 0; d--) { + splitSource.splice(lineNumber, 0, "#endif //" + entry[d]); + } + } + } + + console.log(compileSource()); + + if (first === true) { + var versionThree = "#version 300 es"; + var foundVersion = false; + for (number = 0; number < splitSource.length; number++) { + if (splitSource[number].search(/#version/) != -1) { + splitSource[number] = versionThree; + foundVersion = true; + } + } + + if (!foundVersion) { + splitSource.splice(0, 0, versionThree); + } + } + + removeExtension("EXT_draw_buffers"); + removeExtension("EXT_frag_depth"); + + replaceInSource(/texture2D/, "texture"); + replaceInSource(/texture3D/, "texture"); + replaceInSource(/textureCube/, "texture"); + replaceInSource(/gl_FragDepthEXT/, "gl_FragDepth"); + + if (isFragmentShader) { + replaceInSource(/varying/, "in"); + } else { + replaceInSource(/attribute/, "in"); + replaceInSource(/varying/, "out"); + } + + return compileSource(); + } + + function glslModernizeShaderProgram(context, shaderProgram) { + var vsSource = shaderProgram.vertexShaderSource.clone(); + var fsSource = shaderProgram.fragmentShaderSource.clone(); + + glslModernizeShaderSource(vsSource, false); + glslModernizeShaderSource(fsSource, true); + + var newShaderProgramOptions = { + vertexShaderSource : vsSource, + fragmentShaderSource : fsSource, + gl : shaderProgram._gl, + logShaderCompilation : shaderProgram._logShaderCompilation, + attributeLocations : shaderProgram._attributeLocations + }; + + return context.shaderCache.getShaderProgram(newShaderProgramOptions); + } + + var GLSLModernizer = { + glslModernizeShaderText : glslModernizeShaderText, + glslModernizeShaderSource : glslModernizeShaderSource, + glslModernizeShaderProgram : glslModernizeShaderProgram + }; + + return GLSLModernizer; +}); diff --git a/Source/Renderer/ShaderCache.js b/Source/Renderer/ShaderCache.js index d8635213de6d..1720fd5c3f51 100644 --- a/Source/Renderer/ShaderCache.js +++ b/Source/Renderer/ShaderCache.js @@ -96,8 +96,8 @@ define([ }); } - var vertexShaderText = vertexShaderSource.createCombinedVertexShader(); - var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(); + var vertexShaderText = vertexShaderSource.createCombinedVertexShader(this._context.webgl2); + var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(this._context.webgl2); var keyword = vertexShaderText + fragmentShaderText + JSON.stringify(attributeLocations); var cachedShader; @@ -169,10 +169,11 @@ define([ }); } - var vertexShaderText = vertexShaderSource.createCombinedVertexShader(); - var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(); - var context = this._context; + + var vertexShaderText = vertexShaderSource.createCombinedVertexShader(context.webgl2); + var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(context.webgl2); + var derivedShaderProgram = new ShaderProgram({ gl : context._gl, logShaderCompilation : context.logShaderCompilation, diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 36bb4637145b..b99ff5ee75ec 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -2,12 +2,14 @@ define([ '../Core/defaultValue', '../Core/defined', '../Core/DeveloperError', + '../Renderer/GLSLModernizer', '../Shaders/Builtin/CzmBuiltins', './AutomaticUniforms' ], function( defaultValue, defined, DeveloperError, + GLSLModernizer, CzmBuiltins, AutomaticUniforms) { 'use strict'; @@ -151,7 +153,7 @@ define([ return builtinsSource.replace(root.glslSource, ''); } - function combineShader(shaderSource, isFragmentShader) { + function combineShader(shaderSource, isFragmentShader, webgl2) { var i; var length; @@ -186,6 +188,17 @@ define([ return '\n'; }); + // Extract existing shader version from sources + var extensions = []; + combinedSources = combinedSources.replace(/#extension\s+(.*?)\n/gm, function(match, group1) { + // Extract extension to put at the top + extensions.push(match); + + // Replace original #extension directive with a new line so the line numbers + // are not off by one. + return '\n'; + }); + // Remove precision qualifier combinedSources = combinedSources.replace(/precision\s(lowp|mediump|highp)\s(float|int);/, ''); @@ -204,6 +217,10 @@ define([ result = '#version ' + version + '\n'; } + for (i = 0; i < extensions.length; i++) { + result += extensions[i]; + } + if (isFragmentShader) { result += '\ #ifdef GL_FRAGMENT_PRECISION_HIGH\n\ @@ -224,6 +241,8 @@ define([ } } + result += '#define OUTPUT_DECLARATION\n\n'; + // append built-ins if (shaderSource.includeBuiltIns) { result += getBuiltinsAndAutomaticUniforms(combinedSources); @@ -235,6 +254,13 @@ define([ // append actual source result += combinedSources; + // modernize the source + if (webgl2) { + result = GLSLModernizer.glslModernizeShaderText(result, + isFragmentShader, + true); + } + return result; } @@ -297,19 +323,23 @@ define([ /** * Create a single string containing the full, combined vertex shader with all dependencies and defines. * + * @param {Boolean} [webgl2] A boolean parameter which is true if the context is using WebGL 2, false otherwise + * * @returns {String} The combined shader string. */ - ShaderSource.prototype.createCombinedVertexShader = function() { - return combineShader(this, false); + ShaderSource.prototype.createCombinedVertexShader = function(webgl2) { + return combineShader(this, false, webgl2); }; /** * Create a single string containing the full, combined fragment shader with all dependencies and defines. * + * @param {Boolean} [webgl2] A boolean parameter which is true if the context is using WebGL 2, false otherwise + * * @returns {String} The combined shader string. */ - ShaderSource.prototype.createCombinedFragmentShader = function() { - return combineShader(this, true); + ShaderSource.prototype.createCombinedFragmentShader = function(webgl2) { + return combineShader(this, true, webgl2); }; /** diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 2de068ea749e..915e73c6d389 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -71,6 +71,20 @@ define([ internalFormat = WebGLConstants.DEPTH_COMPONENT24; } } + + if (pixelDatatype == PixelDatatype.FLOAT) { + switch (pixelFormat) { + case PixelFormat.RGBA: + internalFormat = WebGLConstants.RGBA32F; + break; + case PixelFormat.RG: + internalFormat = WebGLConstants.RG32F; + break; + case PixelFormat.R: + internalFormat = WebGLConstants.R32F; + break; + } + } } //>>includeStart('debug', pragmas.debug); diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index c36fc9737286..34f8bc02c4ca 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -66,7 +66,7 @@ varying vec3 v_mieColor; vec4 sampleAndBlend( vec4 previousColor, - sampler2D texture, + sampler2D textureToSample, vec2 tileTextureCoordinates, vec4 textureCoordinateRectangle, vec4 textureCoordinateTranslationAndScale, @@ -94,7 +94,7 @@ vec4 sampleAndBlend( vec2 translation = textureCoordinateTranslationAndScale.xy; vec2 scale = textureCoordinateTranslationAndScale.zw; vec2 textureCoordinates = tileTextureCoordinates * scale + translation; - vec4 value = texture2D(texture, textureCoordinates); + vec4 value = texture2D(textureToSample, textureCoordinates); vec3 color = value.rgb; float alpha = value.a; diff --git a/Specs/Renderer/ShaderProgramSpec.js b/Specs/Renderer/ShaderProgramSpec.js index e2934ed94b66..bf52c55a8c8e 100644 --- a/Specs/Renderer/ShaderProgramSpec.js +++ b/Specs/Renderer/ShaderProgramSpec.js @@ -65,13 +65,13 @@ defineSuite([ var expectedVSText = new ShaderSource({ sources : [vs] - }).createCombinedVertexShader(); + }).createCombinedVertexShader(context.webgl2); expect(sp._vertexShaderText).toEqual(expectedVSText); var expectedFSText = new ShaderSource({ sources : [fs] - }).createCombinedFragmentShader(); + }).createCombinedFragmentShader(context.webgl2); expect(sp._fragmentShaderText).toEqual(expectedFSText); }); diff --git a/Specs/Renderer/ShaderSourceSpec.js b/Specs/Renderer/ShaderSourceSpec.js index b9105e4b28ed..3fa7fbaef0e5 100644 --- a/Specs/Renderer/ShaderSourceSpec.js +++ b/Specs/Renderer/ShaderSourceSpec.js @@ -9,7 +9,7 @@ defineSuite([ defines : ['A', 'B', ''] }); - var shaderText = source.createCombinedVertexShader(); + var shaderText = source.createCombinedVertexShader(false); expect(shaderText).toContain('#define A'); expect(shaderText).toContain('#define B'); expect(shaderText.match(/#define/g).length).toEqual(2); @@ -19,7 +19,7 @@ defineSuite([ var source = new ShaderSource({ sources : ['void func() {}', 'void main() {}'] }); - var shaderText = source.createCombinedVertexShader(); + var shaderText = source.createCombinedVertexShader(false); expect(shaderText).toContain('#line 0\nvoid func() {}'); expect(shaderText).toContain('#line 0\nvoid main() {}'); }); @@ -29,7 +29,7 @@ defineSuite([ defines : ['A', 'B', ''], sources : ['void func() {}', 'void main() {}'] }); - var shaderText = source.createCombinedVertexShader(); + var shaderText = source.createCombinedVertexShader(false); expect(shaderText).toContain('#define A'); expect(shaderText).toContain('#define B'); expect(shaderText.match(/#define/g).length).toEqual(2); @@ -42,7 +42,7 @@ defineSuite([ sources : ['void main() { gl_FragColor = vec4(1.0); }'], pickColorQualifier : 'uniform' }); - var shaderText = source.createCombinedVertexShader(); + var shaderText = source.createCombinedVertexShader(false); expect(shaderText).toContain('uniform vec4 czm_pickColor;'); expect(shaderText).toContain('gl_FragColor = czm_pickColor;'); }); @@ -52,7 +52,7 @@ defineSuite([ sources : ['void main() { gl_FragColor = vec4(1.0); }'], pickColorQualifier : 'varying' }); - var shaderText = source.createCombinedVertexShader(); + var shaderText = source.createCombinedVertexShader(false); expect(shaderText).toContain('varying vec4 czm_pickColor;'); expect(shaderText).toContain('gl_FragColor = czm_pickColor;'); }); From 9f5361dfc117593de729924307ea0f615938305d Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 14:25:12 -0400 Subject: [PATCH 02/57] Extension/capability flags style fix --- Source/Renderer/Context.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Source/Renderer/Context.js b/Source/Renderer/Context.js index e8af2d025cf2..5110b4265d48 100644 --- a/Source/Renderer/Context.js +++ b/Source/Renderer/Context.js @@ -268,16 +268,14 @@ define([ this._antialias = gl.getContextAttributes().antialias; // Query and initialize extensions - this._standardDerivatives = !!getExtension(gl, ['OES_standard_derivatives']) || this._webgl2; - this._elementIndexUint = !!getExtension(gl, ['OES_element_index_uint']) || this._webgl2; - this._depthTexture = !!getExtension(gl, ['WEBGL_depth_texture', 'WEBKIT_WEBGL_depth_texture']) || this._webgl2; - this._textureFloat = !!getExtension(gl, ['OES_texture_float']) || this._webgl2; - this._fragDepth = !!getExtension(gl, ['EXT_frag_depth']) || this._webgl2; + this._standardDerivatives = !!getExtension(gl, ['OES_standard_derivatives']); + this._elementIndexUint = !!getExtension(gl, ['OES_element_index_uint']); + this._depthTexture = !!getExtension(gl, ['WEBGL_depth_texture', 'WEBKIT_WEBGL_depth_texture']); + this._textureFloat = !!getExtension(gl, ['OES_texture_float']); + this._fragDepth = !!getExtension(gl, ['EXT_frag_depth']); this._debugShaders = getExtension(gl, ['WEBGL_debug_shaders']); - if (this._webgl2) { - this._colorBufferFloat = !!getExtension(gl, ['EXT_color_buffer_float']); - } + this._colorBufferFloat = this._webgl2 && !!getExtension(gl, ['EXT_color_buffer_float']); this._s3tc = !!getExtension(gl, ['WEBGL_compressed_texture_s3tc', 'MOZ_WEBGL_compressed_texture_s3tc', 'WEBKIT_WEBGL_compressed_texture_s3tc']); this._pvrtc = !!getExtension(gl, ['WEBGL_compressed_texture_pvrtc', 'WEBKIT_WEBGL_compressed_texture_pvrtc']); @@ -487,7 +485,7 @@ define([ */ standardDerivatives : { get : function() { - return this._standardDerivatives; + return this._standardDerivatives || this._webgl2; } }, @@ -514,7 +512,7 @@ define([ */ depthTexture : { get : function() { - return this._depthTexture; + return this._depthTexture || this._webgl2; } }, @@ -527,7 +525,7 @@ define([ */ floatingPointTexture : { get : function() { - return this._textureFloat; + return this._textureFloat || this._webgl2; } }, @@ -601,7 +599,7 @@ define([ */ fragmentDepth : { get : function() { - return this._fragDepth; + return this._fragDepth || this._webgl2; } }, From a67ee53eb4ff0da5c4ef8618974895456a8a30ed Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 14:28:24 -0400 Subject: [PATCH 03/57] Formatting changes for defines --- Source/Renderer/GLSLModernizer.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 95c373455510..0e4efe038d6b 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -1,8 +1,7 @@ -/*global define*/ define([ '../Core/DeveloperError', - ], function( - DeveloperError) { + ], function( + DeveloperError) { 'use strict'; /** From 31077665374167ca764e5b57b81eb12ee6e21c80 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 14:37:09 -0400 Subject: [PATCH 04/57] Replaces exampleVariable++ with ++exampleVariable in for loops --- Source/Renderer/GLSLModernizer.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 0e4efe038d6b..9fe358bfd6d2 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -34,7 +34,7 @@ define([ var outputDeclarationLine = -1; var number; - for (number = 0; number < splitSource.length; number++) { + for (number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; if (outputDeclarationRegex.exec(line)) { outputDeclarationLine = number; @@ -43,7 +43,7 @@ define([ }; if (outputDeclarationLine == -1) { - for (number = 0; number < splitSource.length; number++) { + for (number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; if (mainFunctionRegex.exec(line)) { outputDeclarationLine = number; @@ -110,13 +110,13 @@ define([ } function replaceInSource(regex, replacement) { - for (var number = 0; number < splitSource.length; number++) { + for (var number = 0; number < splitSource.length; ++number) { splitSource[number] = safeNameReplace(regex, splitSource[number], replacement); } } function findInSource(regex) { - for (var number = 0; number < splitSource.length; number++) { + for (var number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; if (safeNameFind(regex, line) !== -1) { return true; @@ -127,7 +127,7 @@ define([ function compileSource() { var wholeSource = ""; - for (var number = 0; number < splitSource.length; number++) { + for (var number = 0; number < splitSource.length; ++number) { wholeSource += splitSource[number] + "\n"; } return wholeSource; @@ -139,7 +139,7 @@ define([ } function setUnion(set1, set2) { - for (var a = 0; a < set2.length; a++) { + for (var a = 0; a < set2.length; ++a) { setAdd(set2[a], set1); } } @@ -148,13 +148,13 @@ define([ var variableMap = {}; var negativeMap = {}; - for (var a = 0; a < variablesThatWeCareAbout.length; a++) { + for (var a = 0; a < variablesThatWeCareAbout.length; ++a) { var variableThatWeCareAbout = variablesThatWeCareAbout[a]; variableMap[variableThatWeCareAbout] = [null]; } var stack = []; - for (var i = 0; i < splitSource.length; i++) { + for (var i = 0; i < splitSource.length; ++i) { var line = splitSource[i]; var hasIF = line.search(/(#ifdef|#if)/g) !== -1; var hasELSE = line.search(/#else/g) !== -1; @@ -176,7 +176,7 @@ define([ } else if (hasENDIF) { stack.pop(); } else if (line.search(/layout/g) === -1) { - for (a = 0; a < variablesThatWeCareAbout.length; a++) { + for (a = 0; a < variablesThatWeCareAbout.length; ++a) { var care = variablesThatWeCareAbout[a]; if (line.indexOf(care) !== -1) { if (variableMap[care].length === 1 && @@ -197,7 +197,7 @@ define([ if (variableMap.hasOwnProperty(care)) { var entry = variableMap[care]; var toDelete = []; - for (var b = 0; b < entry.length; b++) { + for (var b = 0; b < entry.length; ++b) { var item = entry[b]; if (entry.indexOf(negativeMap[item]) !== -1) { toDelete.push(item); @@ -205,7 +205,7 @@ define([ } } - for (var c = 0; c < toDelete.length; c++) { + for (var c = 0; c < toDelete.length; ++c) { var ind = entry.indexOf(toDelete[c]); if (ind !== -1) { entry.splice(ind, 1); From 15433f3f190392be1d62ef81cd14fe875c394673 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 14:41:14 -0400 Subject: [PATCH 05/57] No more "x characters per line" rule --- Source/Renderer/GLSLModernizer.js | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 9fe358bfd6d2..a06fa7f1eb62 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -14,8 +14,7 @@ define([ */ function glslModernizeShaderSource(shaderSource, isFragmentShader) { for (var i = 0; i < shaderSource.sources.length; i++) { - shaderSource.sources[i] = - glslModernizeShaderText(shaderSource.sources[i], isFragmentShader, i === 0); + shaderSource.sources[i] = glslModernizeShaderText(shaderSource.sources[i], isFragmentShader, i === 0); } shaderSource.defines.push("MODERNIZED"); } @@ -52,9 +51,7 @@ define([ } if (outputDeclarationLine == -1) { - throw new DeveloperError('Could not find a ' + - '#define OUTPUT_DECLARATION ' + - 'nor a main function!'); + throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION nor a main function!'); } function safeNameFalseNegative(regex, region) { @@ -101,11 +98,9 @@ define([ var possiblyFalseNegative = safeNameFalseNegative(regex, region); if (possiblyFalseNegative) { - return region.replace(regex, replacement) + - safeNameReplace(regex, str.substr(endPos), replacement); + return region.replace(regex, replacement) + safeNameReplace(regex, str.substr(endPos), replacement); } else { - return region + - safeNameReplace(regex, str.substr(endPos), replacement); + return region + safeNameReplace(regex, str.substr(endPos), replacement); } } @@ -179,12 +174,10 @@ define([ for (a = 0; a < variablesThatWeCareAbout.length; ++a) { var care = variablesThatWeCareAbout[a]; if (line.indexOf(care) !== -1) { - if (variableMap[care].length === 1 && - variableMap[care][0] === null) { + if (variableMap[care].length === 1 && variableMap[care][0] === null) { variableMap[care] = stack.slice(); } else { - variableMap[care] = variableMap[care].filter( - function(x) { return stack.indexOf(x) >= 0; }); + variableMap[care] = variableMap[care].filter(function(x) { return stack.indexOf(x) >= 0; }); } } } @@ -216,8 +209,7 @@ define([ for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { - if (variableMap.length === 1 && - variableMap[0] === null) { + if (variableMap.length === 1 && variableMap[0] === null) { variableMap.splice(0, 1); } } @@ -240,9 +232,7 @@ define([ if (source.search(fragDataString) != -1) { setAdd(newOutput, variableSet); replaceInSource(regex, newOutput); - splitSource.splice(outputDeclarationLine, 0, - "layout(location = " + i + ") out vec4 " + - newOutput + ";"); + splitSource.splice(outputDeclarationLine, 0, "layout(location = " + i + ") out vec4 " + newOutput + ";"); outputDeclarationLine += 1; } } @@ -261,8 +251,7 @@ define([ var l = splitSource[c]; for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { - var matchVar = - new RegExp("(layout)[^]+(out)[^]+(" + care + ")[^]+", "g"); + var matchVar = new RegExp("(layout)[^]+(out)[^]+(" + care + ")[^]+", "g"); if (matchVar.exec(l) !== null) { lineAdds[l] = care; } From ab57d694b6c063efe06db720d036a755f4d2eba7 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:03:54 -0400 Subject: [PATCH 06/57] Removes the MODERNIZED flag --- Source/Renderer/GLSLModernizer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index a06fa7f1eb62..9ff9999a5b00 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -16,7 +16,6 @@ define([ for (var i = 0; i < shaderSource.sources.length; i++) { shaderSource.sources[i] = glslModernizeShaderText(shaderSource.sources[i], isFragmentShader, i === 0); } - shaderSource.defines.push("MODERNIZED"); } // Note that this function requires the presence of the From cf3aaece8f60f1843d7983f96c8f7a90a732cac3 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:06:35 -0400 Subject: [PATCH 07/57] Removes unnecessary console.log --- Source/Renderer/GLSLModernizer.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 9ff9999a5b00..811620d07a14 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -275,8 +275,6 @@ define([ } } - console.log(compileSource()); - if (first === true) { var versionThree = "#version 300 es"; var foundVersion = false; From 3932bf30a76c158de72ac1e27d5819d9717e8413 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:21:20 -0400 Subject: [PATCH 08/57] Replaces search() with test() --- Source/Renderer/GLSLModernizer.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 811620d07a14..4eee386f89f6 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -150,16 +150,16 @@ define([ var stack = []; for (var i = 0; i < splitSource.length; ++i) { var line = splitSource[i]; - var hasIF = line.search(/(#ifdef|#if)/g) !== -1; - var hasELSE = line.search(/#else/g) !== -1; - var hasENDIF = line.search(/#endif/g) !== -1; + var hasIF = /(#ifdef|#if)/g.test(line); + var hasELSE = /#else/g.test(line); + var hasENDIF = /#endif/g.test(line); if (hasIF) { stack.push(line); } else if (hasELSE) { var top = stack[stack.length - 1]; var op = top.replace("ifdef", "ifndef"); - if (op.search(/if/g) !== -1) { + if (/if/g.test(op)) { op = op.replace(/(#if\s+)(\S*)([^]*)/, "$1!($2)$3"); } stack.pop(); @@ -169,7 +169,7 @@ define([ negativeMap[op] = top; } else if (hasENDIF) { stack.pop(); - } else if (line.search(/layout/g) === -1) { + } else if (!/layout/g.test(line)) { for (a = 0; a < variablesThatWeCareAbout.length; ++a) { var care = variablesThatWeCareAbout[a]; if (line.indexOf(care) !== -1) { @@ -228,7 +228,7 @@ define([ var fragDataString = "gl_FragData\\[" + i + "\\]"; var newOutput = 'czm_out' + i; var regex = new RegExp(fragDataString, "g"); - if (source.search(fragDataString) != -1) { + if (regex.test(source)) { setAdd(newOutput, variableSet); replaceInSource(regex, newOutput); splitSource.splice(outputDeclarationLine, 0, "layout(location = " + i + ") out vec4 " + newOutput + ";"); @@ -279,7 +279,7 @@ define([ var versionThree = "#version 300 es"; var foundVersion = false; for (number = 0; number < splitSource.length; number++) { - if (splitSource[number].search(/#version/) != -1) { + if (/#version/.test(splitSource[number])) { splitSource[number] = versionThree; foundVersion = true; } From 7a844e10dbfa4b61343d8fcf7c30998c445d1bc1 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:33:48 -0400 Subject: [PATCH 09/57] Now passing context into ShaderSource instead of context.webgl2 --- Source/Renderer/ShaderCache.js | 8 ++++---- Source/Renderer/ShaderSource.js | 16 ++++++++-------- Specs/Renderer/ShaderSourceSpec.js | 24 ++++++++++++++++++------ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Source/Renderer/ShaderCache.js b/Source/Renderer/ShaderCache.js index 1720fd5c3f51..70092c6954f0 100644 --- a/Source/Renderer/ShaderCache.js +++ b/Source/Renderer/ShaderCache.js @@ -96,8 +96,8 @@ define([ }); } - var vertexShaderText = vertexShaderSource.createCombinedVertexShader(this._context.webgl2); - var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(this._context.webgl2); + var vertexShaderText = vertexShaderSource.createCombinedVertexShader(this._context); + var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(this._context); var keyword = vertexShaderText + fragmentShaderText + JSON.stringify(attributeLocations); var cachedShader; @@ -171,8 +171,8 @@ define([ var context = this._context; - var vertexShaderText = vertexShaderSource.createCombinedVertexShader(context.webgl2); - var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(context.webgl2); + var vertexShaderText = vertexShaderSource.createCombinedVertexShader(context); + var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(context); var derivedShaderProgram = new ShaderProgram({ gl : context._gl, diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index b99ff5ee75ec..f8f9b9e843b3 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -153,7 +153,7 @@ define([ return builtinsSource.replace(root.glslSource, ''); } - function combineShader(shaderSource, isFragmentShader, webgl2) { + function combineShader(shaderSource, isFragmentShader, context) { var i; var length; @@ -255,7 +255,7 @@ define([ result += combinedSources; // modernize the source - if (webgl2) { + if (context.webgl2) { result = GLSLModernizer.glslModernizeShaderText(result, isFragmentShader, true); @@ -323,23 +323,23 @@ define([ /** * Create a single string containing the full, combined vertex shader with all dependencies and defines. * - * @param {Boolean} [webgl2] A boolean parameter which is true if the context is using WebGL 2, false otherwise + * @param {Object} [context] The current rendering context * * @returns {String} The combined shader string. */ - ShaderSource.prototype.createCombinedVertexShader = function(webgl2) { - return combineShader(this, false, webgl2); + ShaderSource.prototype.createCombinedVertexShader = function(context) { + return combineShader(this, false, context); }; /** * Create a single string containing the full, combined fragment shader with all dependencies and defines. * - * @param {Boolean} [webgl2] A boolean parameter which is true if the context is using WebGL 2, false otherwise + * @param {Object} [context] The current rendering context * * @returns {String} The combined shader string. */ - ShaderSource.prototype.createCombinedFragmentShader = function(webgl2) { - return combineShader(this, true, webgl2); + ShaderSource.prototype.createCombinedFragmentShader = function(context) { + return combineShader(this, true, context); }; /** diff --git a/Specs/Renderer/ShaderSourceSpec.js b/Specs/Renderer/ShaderSourceSpec.js index 3fa7fbaef0e5..912d45c5d05f 100644 --- a/Specs/Renderer/ShaderSourceSpec.js +++ b/Specs/Renderer/ShaderSourceSpec.js @@ -9,7 +9,9 @@ defineSuite([ defines : ['A', 'B', ''] }); - var shaderText = source.createCombinedVertexShader(false); + var shaderText = source.createCombinedVertexShader({ + webgl2: false + }); expect(shaderText).toContain('#define A'); expect(shaderText).toContain('#define B'); expect(shaderText.match(/#define/g).length).toEqual(2); @@ -19,7 +21,9 @@ defineSuite([ var source = new ShaderSource({ sources : ['void func() {}', 'void main() {}'] }); - var shaderText = source.createCombinedVertexShader(false); + var shaderText = source.createCombinedVertexShader({ + webgl2: false + }); expect(shaderText).toContain('#line 0\nvoid func() {}'); expect(shaderText).toContain('#line 0\nvoid main() {}'); }); @@ -29,7 +33,9 @@ defineSuite([ defines : ['A', 'B', ''], sources : ['void func() {}', 'void main() {}'] }); - var shaderText = source.createCombinedVertexShader(false); + var shaderText = source.createCombinedVertexShader({ + webgl2: false + }); expect(shaderText).toContain('#define A'); expect(shaderText).toContain('#define B'); expect(shaderText.match(/#define/g).length).toEqual(2); @@ -42,7 +48,9 @@ defineSuite([ sources : ['void main() { gl_FragColor = vec4(1.0); }'], pickColorQualifier : 'uniform' }); - var shaderText = source.createCombinedVertexShader(false); + var shaderText = source.createCombinedVertexShader({ + webgl2: false + }); expect(shaderText).toContain('uniform vec4 czm_pickColor;'); expect(shaderText).toContain('gl_FragColor = czm_pickColor;'); }); @@ -52,7 +60,9 @@ defineSuite([ sources : ['void main() { gl_FragColor = vec4(1.0); }'], pickColorQualifier : 'varying' }); - var shaderText = source.createCombinedVertexShader(false); + var shaderText = source.createCombinedVertexShader({ + webgl2: false + }); expect(shaderText).toContain('varying vec4 czm_pickColor;'); expect(shaderText).toContain('gl_FragColor = czm_pickColor;'); }); @@ -69,7 +79,9 @@ defineSuite([ var source = new ShaderSource({ sources : ['#version 300 es\nvoid main() {gl_FragColor = vec4(1.0); }'] }); - var shaderText = source.createCombinedVertexShader(); + var shaderText = source.createCombinedVertexShader(({ + webgl2: false + }); expect(shaderText).toStartWith('#version 300 es\n'); }); }); From a16652fc034e9aa986c8a57e7386bbbfe7ea7d41 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:35:18 -0400 Subject: [PATCH 10/57] Fixes comment --- Source/Renderer/ShaderSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index f8f9b9e843b3..1d58052b4853 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -188,7 +188,7 @@ define([ return '\n'; }); - // Extract existing shader version from sources + // Extract shader extensions from sources var extensions = []; combinedSources = combinedSources.replace(/#extension\s+(.*?)\n/gm, function(match, group1) { // Extract extension to put at the top From 9b076e74aff15f65d5dec58723e4ca9e7f75a88d Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:39:33 -0400 Subject: [PATCH 11/57] Slightly more concise regex --- Source/Renderer/ShaderSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 1d58052b4853..4b67f0b34e2e 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -190,7 +190,7 @@ define([ // Extract shader extensions from sources var extensions = []; - combinedSources = combinedSources.replace(/#extension\s+(.*?)\n/gm, function(match, group1) { + combinedSources = combinedSources.replace(/#extension.*\n/gm, function(match) { // Extract extension to put at the top extensions.push(match); From 0901e1d082b481e0d5c04e64028b1b76c949310f Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:40:40 -0400 Subject: [PATCH 12/57] Fixes length check --- Source/Renderer/ShaderSource.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 4b67f0b34e2e..972621f188e4 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -217,7 +217,8 @@ define([ result = '#version ' + version + '\n'; } - for (i = 0; i < extensions.length; i++) { + var extensionsLength = extensions.length; + for (i = 0; i < extensionsLength; i++) { result += extensions[i]; } From 97db097ff3a51395b8601de4fe93b58c86a916af Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:41:26 -0400 Subject: [PATCH 13/57] Formatting fixes --- Source/Renderer/ShaderSource.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 972621f188e4..c69a6b38e35c 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -257,9 +257,7 @@ define([ // modernize the source if (context.webgl2) { - result = GLSLModernizer.glslModernizeShaderText(result, - isFragmentShader, - true); + result = GLSLModernizer.glslModernizeShaderText(result, isFragmentShader, true); } return result; From 78fa9bb0369ae92373066bd3f9bc1499e4070275 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:42:30 -0400 Subject: [PATCH 14/57] Fixes switch statement case indentation --- Source/Renderer/Texture.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 915e73c6d389..910ed3369a87 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -74,15 +74,15 @@ define([ if (pixelDatatype == PixelDatatype.FLOAT) { switch (pixelFormat) { - case PixelFormat.RGBA: - internalFormat = WebGLConstants.RGBA32F; - break; - case PixelFormat.RG: - internalFormat = WebGLConstants.RG32F; - break; - case PixelFormat.R: - internalFormat = WebGLConstants.R32F; - break; + case PixelFormat.RGBA: + internalFormat = WebGLConstants.RGBA32F; + break; + case PixelFormat.RG: + internalFormat = WebGLConstants.RG32F; + break; + case PixelFormat.R: + internalFormat = WebGLConstants.R32F; + break; } } } From bf07d1c178f1a85c1db6e505f2842255fca79789 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:43:54 -0400 Subject: [PATCH 15/57] Changes the last few == operators to === operators --- Source/Renderer/GLSLModernizer.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 4eee386f89f6..f387459fa069 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -40,7 +40,7 @@ define([ } }; - if (outputDeclarationLine == -1) { + if (outputDeclarationLine === -1) { for (number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; if (mainFunctionRegex.exec(line)) { @@ -49,7 +49,7 @@ define([ }; } - if (outputDeclarationLine == -1) { + if (outputDeclarationLine === -1) { throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION nor a main function!'); } @@ -68,7 +68,7 @@ define([ function safeNameFind(regex, str) { var originalMatch = regex.exec(str); - if (originalMatch == null) { + if (originalMatch === null) { return -1; } var endPos = originalMatch.index + originalMatch[0].length; @@ -79,7 +79,7 @@ define([ return endPos; } else { var childResult = safeNameFind(regex, str.substring(endPos)); - if (childResult == -1) { + if (childResult === -1) { return -1; } else { return endPos + childResult; @@ -89,7 +89,7 @@ define([ function safeNameReplace(regex, str, replacement) { var originalMatch = regex.exec(str); - if (originalMatch == null) { + if (originalMatch === null) { return str; } var endPos = originalMatch.index + originalMatch[0].length; From 5ad9b82e3c2e17f4c328e841b111d2e2966652ee Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Wed, 12 Jul 2017 15:50:45 -0400 Subject: [PATCH 16/57] Adds support for PixelFormat.RGB textures --- Source/Renderer/Texture.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 910ed3369a87..2edf6062ecfd 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -77,6 +77,9 @@ define([ case PixelFormat.RGBA: internalFormat = WebGLConstants.RGBA32F; break; + case PixelFormat.RGB: + internalFormat = WebGLConstants.RGB32F; + break; case PixelFormat.RG: internalFormat = WebGLConstants.RG32F; break; From 0ef293938f9193629220124a19d96d8a7c8f2955 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:05:42 -0400 Subject: [PATCH 17/57] Creates mockContext variable --- Specs/Renderer/ShaderSourceSpec.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/Specs/Renderer/ShaderSourceSpec.js b/Specs/Renderer/ShaderSourceSpec.js index 912d45c5d05f..461819a070f1 100644 --- a/Specs/Renderer/ShaderSourceSpec.js +++ b/Specs/Renderer/ShaderSourceSpec.js @@ -4,14 +4,16 @@ defineSuite([ ShaderSource) { 'use strict'; + var mockContext = { + webgl2 : false + }; + it('combines #defines', function() { var source = new ShaderSource({ defines : ['A', 'B', ''] }); - var shaderText = source.createCombinedVertexShader({ - webgl2: false - }); + var shaderText = source.createCombinedVertexShader(mockContext); expect(shaderText).toContain('#define A'); expect(shaderText).toContain('#define B'); expect(shaderText.match(/#define/g).length).toEqual(2); @@ -21,9 +23,7 @@ defineSuite([ var source = new ShaderSource({ sources : ['void func() {}', 'void main() {}'] }); - var shaderText = source.createCombinedVertexShader({ - webgl2: false - }); + var shaderText = source.createCombinedVertexShader(mockContext); expect(shaderText).toContain('#line 0\nvoid func() {}'); expect(shaderText).toContain('#line 0\nvoid main() {}'); }); @@ -33,9 +33,7 @@ defineSuite([ defines : ['A', 'B', ''], sources : ['void func() {}', 'void main() {}'] }); - var shaderText = source.createCombinedVertexShader({ - webgl2: false - }); + var shaderText = source.createCombinedVertexShader(mockContext); expect(shaderText).toContain('#define A'); expect(shaderText).toContain('#define B'); expect(shaderText.match(/#define/g).length).toEqual(2); @@ -48,9 +46,7 @@ defineSuite([ sources : ['void main() { gl_FragColor = vec4(1.0); }'], pickColorQualifier : 'uniform' }); - var shaderText = source.createCombinedVertexShader({ - webgl2: false - }); + var shaderText = source.createCombinedVertexShader(mockContext); expect(shaderText).toContain('uniform vec4 czm_pickColor;'); expect(shaderText).toContain('gl_FragColor = czm_pickColor;'); }); @@ -60,9 +56,7 @@ defineSuite([ sources : ['void main() { gl_FragColor = vec4(1.0); }'], pickColorQualifier : 'varying' }); - var shaderText = source.createCombinedVertexShader({ - webgl2: false - }); + var shaderText = source.createCombinedVertexShader(mockContext); expect(shaderText).toContain('varying vec4 czm_pickColor;'); expect(shaderText).toContain('gl_FragColor = czm_pickColor;'); }); @@ -79,9 +73,7 @@ defineSuite([ var source = new ShaderSource({ sources : ['#version 300 es\nvoid main() {gl_FragColor = vec4(1.0); }'] }); - var shaderText = source.createCombinedVertexShader(({ - webgl2: false - }); + var shaderText = source.createCombinedVertexShader(mockContext); expect(shaderText).toStartWith('#version 300 es\n'); }); }); From da577015d57eafea94d95e3ec44f4be37ec474d4 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:06:35 -0400 Subject: [PATCH 18/57] Now passing in context rather than context.webgl2 for test in ShaderProgramSpec --- Specs/Renderer/ShaderProgramSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/Renderer/ShaderProgramSpec.js b/Specs/Renderer/ShaderProgramSpec.js index bf52c55a8c8e..dc5a9b5e190d 100644 --- a/Specs/Renderer/ShaderProgramSpec.js +++ b/Specs/Renderer/ShaderProgramSpec.js @@ -65,7 +65,7 @@ defineSuite([ var expectedVSText = new ShaderSource({ sources : [vs] - }).createCombinedVertexShader(context.webgl2); + }).createCombinedVertexShader(context); expect(sp._vertexShaderText).toEqual(expectedVSText); From fe785356d1be97d85bb0125101ca99453bb9c802 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:07:04 -0400 Subject: [PATCH 19/57] Fixes equality operator --- Source/Renderer/Texture.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/Texture.js b/Source/Renderer/Texture.js index 2edf6062ecfd..34d71a6d56a6 100644 --- a/Source/Renderer/Texture.js +++ b/Source/Renderer/Texture.js @@ -72,7 +72,7 @@ define([ } } - if (pixelDatatype == PixelDatatype.FLOAT) { + if (pixelDatatype === PixelDatatype.FLOAT) { switch (pixelFormat) { case PixelFormat.RGBA: internalFormat = WebGLConstants.RGBA32F; From 2ddc44201617843252cb6f548133849b5e3bf55d Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:09:17 -0400 Subject: [PATCH 20/57] Adds explanatory comment for OUTPUT_DECLARATION --- Source/Renderer/ShaderSource.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index c69a6b38e35c..5eb6bdd5cb5e 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -242,6 +242,8 @@ define([ } } + // GLSLModernizer inserts its own layout qualifiers + // at this position in the source result += '#define OUTPUT_DECLARATION\n\n'; // append built-ins From f76f1998bd617c3623b221df9e69a5a34cb49e39 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:15:02 -0400 Subject: [PATCH 21/57] Removes unnecessary functions in GLSLModernizer --- Source/Renderer/GLSLModernizer.js | 61 ++++++++----------------------- Source/Renderer/ShaderSource.js | 4 +- 2 files changed, 17 insertions(+), 48 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index f387459fa069..66f3f814cd58 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -10,18 +10,13 @@ define([ * This function is nowhere near comprehensive or complete. It just * handles some common cases. * + * Note that this function requires the presence of the + * "#define OUTPUT_DECLARATION" line that is appended + * by ShaderSource. + * * @private */ - function glslModernizeShaderSource(shaderSource, isFragmentShader) { - for (var i = 0; i < shaderSource.sources.length; i++) { - shaderSource.sources[i] = glslModernizeShaderText(shaderSource.sources[i], isFragmentShader, i === 0); - } - } - - // Note that this function requires the presence of the - // "#define OUTPUT_DECLARATION" line that is appended - // by ShaderSource - function glslModernizeShaderText(source, isFragmentShader, first) { + function glslModernizeShaderText(source, isFragmentShader) { var mainFunctionRegex = /void\s+main\s*\((void)?\)/; var outputDeclarationRegex = /#define OUTPUT_DECLARATION/; var splitSource = source.split('\n'); @@ -275,19 +270,17 @@ define([ } } - if (first === true) { - var versionThree = "#version 300 es"; - var foundVersion = false; - for (number = 0; number < splitSource.length; number++) { - if (/#version/.test(splitSource[number])) { - splitSource[number] = versionThree; - foundVersion = true; - } + var versionThree = "#version 300 es"; + var foundVersion = false; + for (number = 0; number < splitSource.length; number++) { + if (/#version/.test(splitSource[number])) { + splitSource[number] = versionThree; + foundVersion = true; } + } - if (!foundVersion) { - splitSource.splice(0, 0, versionThree); - } + if (!foundVersion) { + splitSource.splice(0, 0, versionThree); } removeExtension("EXT_draw_buffers"); @@ -308,29 +301,5 @@ define([ return compileSource(); } - function glslModernizeShaderProgram(context, shaderProgram) { - var vsSource = shaderProgram.vertexShaderSource.clone(); - var fsSource = shaderProgram.fragmentShaderSource.clone(); - - glslModernizeShaderSource(vsSource, false); - glslModernizeShaderSource(fsSource, true); - - var newShaderProgramOptions = { - vertexShaderSource : vsSource, - fragmentShaderSource : fsSource, - gl : shaderProgram._gl, - logShaderCompilation : shaderProgram._logShaderCompilation, - attributeLocations : shaderProgram._attributeLocations - }; - - return context.shaderCache.getShaderProgram(newShaderProgramOptions); - } - - var GLSLModernizer = { - glslModernizeShaderText : glslModernizeShaderText, - glslModernizeShaderSource : glslModernizeShaderSource, - glslModernizeShaderProgram : glslModernizeShaderProgram - }; - - return GLSLModernizer; + return glslModernizeShaderText; }); diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 5eb6bdd5cb5e..283f21732642 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -9,7 +9,7 @@ define([ defaultValue, defined, DeveloperError, - GLSLModernizer, + glslModernizeShaderText, CzmBuiltins, AutomaticUniforms) { 'use strict'; @@ -259,7 +259,7 @@ define([ // modernize the source if (context.webgl2) { - result = GLSLModernizer.glslModernizeShaderText(result, isFragmentShader, true); + result = glslModernizeShaderText(result, isFragmentShader, true); } return result; From ffc01096baff07aad12b8eb445cef1c94d2fac09 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:17:03 -0400 Subject: [PATCH 22/57] Fixes some documenation for ShaderSource --- Source/Renderer/ShaderSource.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 283f21732642..642cdc15e0d3 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -324,7 +324,7 @@ define([ /** * Create a single string containing the full, combined vertex shader with all dependencies and defines. * - * @param {Object} [context] The current rendering context + * @param {Context} context The current rendering context * * @returns {String} The combined shader string. */ @@ -335,7 +335,7 @@ define([ /** * Create a single string containing the full, combined fragment shader with all dependencies and defines. * - * @param {Object} [context] The current rendering context + * @param {Context} context The current rendering context * * @returns {String} The combined shader string. */ From f54c550270bf31ed18387aaedd40a8a8b0bfa246 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:17:41 -0400 Subject: [PATCH 23/57] Fixes typo in documentation --- Source/Renderer/GLSLModernizer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 66f3f814cd58..1955c9c439bb 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -5,7 +5,7 @@ define([ 'use strict'; /** - * A function to port GLSL shaders from GLSL ES 1.00 to GLSL ES 2.00 + * A function to port GLSL shaders from GLSL ES 1.00 to GLSL ES 3.00 * * This function is nowhere near comprehensive or complete. It just * handles some common cases. From f0c20d8282ebc5fee199d41c0ccc7111c0cd1beb Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 10:21:58 -0400 Subject: [PATCH 24/57] Removes check for main() --- Source/Renderer/GLSLModernizer.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 1955c9c439bb..fa5a25acccf6 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -17,7 +17,6 @@ define([ * @private */ function glslModernizeShaderText(source, isFragmentShader) { - var mainFunctionRegex = /void\s+main\s*\((void)?\)/; var outputDeclarationRegex = /#define OUTPUT_DECLARATION/; var splitSource = source.split('\n'); @@ -34,15 +33,6 @@ define([ break; } }; - - if (outputDeclarationLine === -1) { - for (number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - if (mainFunctionRegex.exec(line)) { - outputDeclarationLine = number; - } - }; - } if (outputDeclarationLine === -1) { throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION nor a main function!'); From 4679559cf5f367859a61674c6bfbdff94505324b Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 11:45:17 -0400 Subject: [PATCH 25/57] Trying to remove all the safeName* --- Source/Renderer/GLSLModernizer.js | 50 ++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index fa5a25acccf6..22fe87c225ce 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -1,6 +1,8 @@ define([ + '../Core/defined', '../Core/DeveloperError', ], function( + defined, DeveloperError) { 'use strict'; @@ -88,16 +90,35 @@ define([ } } - function replaceInSource(regex, replacement) { + function replaceInSource(str, replacement, strIsRegex) { + var replaceAll = function(target, search, replacement) { + return target.split(search).join(replacement); + }; + + var regex = str; + if (!defined(strIsRegex) || strIsRegex === false) { + /*var regexStr = "(?:^.*[^\\w](" + str + + ")[^\\w].*$|^(" + str + + ")[^\\w].*$|^.*[^\\w](" + str + + ")$|^(" + str + ")$)"; + regex = new RegExp(regexStr, 'g');*/ + regex = new RegExp(str, 'g'); + } for (var number = 0; number < splitSource.length; ++number) { splitSource[number] = safeNameReplace(regex, splitSource[number], replacement); + //splitSource[number] = replaceAll(splitSource[number], regex, replacement); } } - function findInSource(regex) { + function findInSource(str) { + var regexStr = "(^.*[^\\w](" + str + + ")[^\\w].*$|^(" + str + + ")[^\\w].*$|^.*[^\\w](" + str + + ")$|^(" + str + ")$)"; + var regex = new RegExp(regexStr, 'g'); for (var number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; - if (safeNameFind(regex, line) !== -1) { + if (regex.test(line)) { return true; } } @@ -204,7 +225,7 @@ define([ function removeExtension(name) { var regex = "#extension\\s+GL_" + name + "\\s+:\\s+[a-zA-Z0-9]+\\s*$"; - replaceInSource(new RegExp(regex, "g"), ""); + replaceInSource(new RegExp(regex, "g"), "", true); } var variableSet = []; @@ -215,16 +236,16 @@ define([ var regex = new RegExp(fragDataString, "g"); if (regex.test(source)) { setAdd(newOutput, variableSet); - replaceInSource(regex, newOutput); + replaceInSource(fragDataString, newOutput); splitSource.splice(outputDeclarationLine, 0, "layout(location = " + i + ") out vec4 " + newOutput + ";"); outputDeclarationLine += 1; } } var czmFragColor = "czm_fragColor"; - if (findInSource(/gl_FragColor/)) { + if (findInSource("gl_FragColor")) { setAdd(czmFragColor, variableSet); - replaceInSource(/gl_FragColor/, czmFragColor); + replaceInSource("gl_FragColor", czmFragColor); splitSource.splice(outputDeclarationLine, 0, "layout(location = 0) out vec4 czm_fragColor;"); outputDeclarationLine += 1; } @@ -276,18 +297,19 @@ define([ removeExtension("EXT_draw_buffers"); removeExtension("EXT_frag_depth"); - replaceInSource(/texture2D/, "texture"); - replaceInSource(/texture3D/, "texture"); - replaceInSource(/textureCube/, "texture"); - replaceInSource(/gl_FragDepthEXT/, "gl_FragDepth"); + replaceInSource("texture2D", "texture"); + replaceInSource("texture3D", "texture"); + replaceInSource("textureCube", "texture"); + replaceInSource("gl_FragDepthEXT", "gl_FragDepth"); if (isFragmentShader) { - replaceInSource(/varying/, "in"); + replaceInSource("varying", "in"); } else { - replaceInSource(/attribute/, "in"); - replaceInSource(/varying/, "out"); + replaceInSource("attribute", "in"); + replaceInSource("varying", "out"); } + console.log(compileSource()); return compileSource(); } From 217acfefee123561f4738505e62c2ed7cd9093c1 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 14:37:42 -0400 Subject: [PATCH 26/57] Now using a more comprehensive regex rather than the old recursive solution --- Source/Renderer/GLSLModernizer.js | 75 ++++--------------------------- 1 file changed, 8 insertions(+), 67 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 22fe87c225ce..32efe943a6f3 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -39,82 +39,24 @@ define([ if (outputDeclarationLine === -1) { throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION nor a main function!'); } - - function safeNameFalseNegative(regex, region) { - var regExpStr = regex.toString(); - regExpStr = regExpStr.match(/\/([^\/]+)(\/.)?/)[1]; - var somethingBadInString = - new RegExp("[a-zA-Z0-9_]+" + regExpStr, 'g'); - var searchResult = region.search(somethingBadInString); - if (searchResult === -1) { - return true; - } else { - return false; - } - } - - function safeNameFind(regex, str) { - var originalMatch = regex.exec(str); - if (originalMatch === null) { - return -1; - } - var endPos = originalMatch.index + originalMatch[0].length; - var region = str.substring(0, endPos); - - var possiblyFalseNegative = safeNameFalseNegative(regex, region); - if (possiblyFalseNegative) { - return endPos; - } else { - var childResult = safeNameFind(regex, str.substring(endPos)); - if (childResult === -1) { - return -1; - } else { - return endPos + childResult; - } - } - } - - function safeNameReplace(regex, str, replacement) { - var originalMatch = regex.exec(str); - if (originalMatch === null) { - return str; - } - var endPos = originalMatch.index + originalMatch[0].length; - var region = str.substring(0, endPos); - - var possiblyFalseNegative = safeNameFalseNegative(regex, region); - if (possiblyFalseNegative) { - return region.replace(regex, replacement) + safeNameReplace(regex, str.substr(endPos), replacement); - } else { - return region + safeNameReplace(regex, str.substr(endPos), replacement); - } - } + // Note that this fails if your string looks like + // searchString[singleCharacter]searchString function replaceInSource(str, replacement, strIsRegex) { - var replaceAll = function(target, search, replacement) { - return target.split(search).join(replacement); - }; - var regex = str; if (!defined(strIsRegex) || strIsRegex === false) { - /*var regexStr = "(?:^.*[^\\w](" + str + - ")[^\\w].*$|^(" + str + - ")[^\\w].*$|^.*[^\\w](" + str + - ")$|^(" + str + ")$)"; - regex = new RegExp(regexStr, 'g');*/ - regex = new RegExp(str, 'g'); + var regexStr = "(^|[^\\w])(" + str + ")($|[^\\w])"; + regex = new RegExp(regexStr, 'g'); } for (var number = 0; number < splitSource.length; ++number) { - splitSource[number] = safeNameReplace(regex, splitSource[number], replacement); - //splitSource[number] = replaceAll(splitSource[number], regex, replacement); + splitSource[number] = splitSource[number].replace(regex, function (match, group1, group2, group3) { + return group1 + replacement + group2; + }); } } function findInSource(str) { - var regexStr = "(^.*[^\\w](" + str + - ")[^\\w].*$|^(" + str + - ")[^\\w].*$|^.*[^\\w](" + str + - ")$|^(" + str + ")$)"; + var regexStr = "(^|[^\\w])(" + str + ")($|[^\\w])"; var regex = new RegExp(regexStr, 'g'); for (var number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; @@ -309,7 +251,6 @@ define([ replaceInSource("varying", "out"); } - console.log(compileSource()); return compileSource(); } From 39c2c2a851881b87c06214e91df229b78c3cfb01 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 15:37:16 -0400 Subject: [PATCH 27/57] Bugfix in replaceInSource --- Source/Renderer/GLSLModernizer.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 32efe943a6f3..4050067df2b5 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -44,14 +44,18 @@ define([ // searchString[singleCharacter]searchString function replaceInSource(str, replacement, strIsRegex) { var regex = str; - if (!defined(strIsRegex) || strIsRegex === false) { + if ((strIsRegex === undefined) || strIsRegex === false) { var regexStr = "(^|[^\\w])(" + str + ")($|[^\\w])"; regex = new RegExp(regexStr, 'g'); } + for (var number = 0; number < splitSource.length; ++number) { - splitSource[number] = splitSource[number].replace(regex, function (match, group1, group2, group3) { - return group1 + replacement + group2; - }); + var line = splitSource[number]; + if (strIsRegex) { + splitSource[number] = line.replace(regex, replacement); + } else { + splitSource[number] = line.replace(regex, "$1" + replacement + "$3"); + } } } From 405951dc135db42cda920ecd778a2b836e544d35 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 15:40:38 -0400 Subject: [PATCH 28/57] Fixes floatingPointTexture flag false positive in WebGL 2 --- Source/Renderer/Context.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/Context.js b/Source/Renderer/Context.js index 5110b4265d48..b92bfa2e916c 100644 --- a/Source/Renderer/Context.js +++ b/Source/Renderer/Context.js @@ -525,7 +525,7 @@ define([ */ floatingPointTexture : { get : function() { - return this._textureFloat || this._webgl2; + return this._textureFloat || this._colorBufferFloat; } }, From 9f4f84c0b3c088cdbeec87bcd9db15e0c28541e2 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 16:13:18 -0400 Subject: [PATCH 29/57] Adds first test for GLSLModernizer --- Specs/Renderer/GLSLModernizerSpec.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Specs/Renderer/GLSLModernizerSpec.js diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js new file mode 100644 index 000000000000..1d7e8618f5e4 --- /dev/null +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -0,0 +1,21 @@ +defineSuite([ + 'Renderer/GLSLModernizer' + ], function( + glslModernizeShaderText) { + 'use strict'; + + it ('adds version string', function() { + var simple = '#define OUTPUT_DECLARATION \n' + + 'void main() \n' + + '{ \n' + + '} \n'; + var output = glslModernizeShaderText(simple, true); + var expected = '#version 300 es\n' + + '#define OUTPUT_DECLARATION \n' + + 'void main() \n' + + '{ \n' + + '} \n' + + '\n'; + expect(output).toEqual(expected); + }); +}); From 4745e84b498cac1ee05bcd67f2f569a48e48ead9 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 16:34:05 -0400 Subject: [PATCH 30/57] Adds a few more tests, mostly for layout qualifiers --- Specs/Renderer/GLSLModernizerSpec.js | 85 ++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js index 1d7e8618f5e4..837a545bef22 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -5,17 +5,92 @@ defineSuite([ 'use strict'; it ('adds version string', function() { - var simple = '#define OUTPUT_DECLARATION \n' + + var simple = + '#define OUTPUT_DECLARATION \n' + 'void main() \n' + '{ \n' + '} \n'; var output = glslModernizeShaderText(simple, true); - var expected = '#version 300 es\n' + + var expected = '#version 300 es'; + expect(output.split('\n')[0]).toEqual(expected); + }); + + it ('removes extensions', function() { + var extensions = '#define OUTPUT_DECLARATION \n' + + '#extension GL_EXT_draw_buffers : enable \n' + 'void main() \n' + '{ \n' + - '} \n' + - '\n'; - expect(output).toEqual(expected); + '} \n'; + var output = glslModernizeShaderText(extensions, true); + var notExpected = '#extension GL_EXT_draw_buffers : enable \n'; + expect(output).not.toContain(notExpected); + }); + + it ('throws exception if no output declaration', function() { + var noOutputDeclaration = + 'void main() \n' + + '{ \n' + + '} \n'; + var runFunc = function() { + glslModernizeShaderText(noOutputDeclaration, true); + }; + expect(runFunc).toThrow(); + }); + + it ('creates layout qualifier for gl_FragColor', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + 'void main() \n' + + '{ \n' + + ' gl_FragColor = vec4(0.0); \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected = 'layout(location = 0) out vec4 czm_fragColor;'; + expect(output).toContain(expected); + }); + + it ('creates layout qualifier for gl_FragData', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + 'void main() \n' + + '{ \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected = 'layout(location = 0) out vec4 czm_out0;'; + expect(output).toContain(expected); + }); + + it ('creates layout qualifier for MRT', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#extension GL_EXT_draw_buffers : enable \n' + + 'void main() \n' + + '{ \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' gl_FragData[1] = vec4(1.0); \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected0 = 'layout(location = 0) out vec4 czm_out0;'; + var expected1 = 'layout(location = 1) out vec4 czm_out1;'; + expect(output).toContain(expected0); + expect(output).toContain(expected1); + }); + + it ('creates layout qualifier under single condition', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' gl_FragData[1] = vec4(1.0); \n' + + ' #endif //EXAMPLE_BRANCH \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + expect(output).toContain(expected); }); }); From 7c58a657a7e9695abef3181900a6d4b956ccd0f5 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 16:49:03 -0400 Subject: [PATCH 31/57] Adds a few more tests --- Specs/Renderer/GLSLModernizerSpec.js | 83 +++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js index 837a545bef22..3e9efc38a91e 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -78,7 +78,7 @@ defineSuite([ expect(output).toContain(expected1); }); - it ('creates layout qualifier under single condition', function() { + it ('creates single layout qualifier under single branch, single condition', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -86,11 +86,90 @@ defineSuite([ '{ \n' + ' #ifdef EXAMPLE_BRANCH \n' + ' gl_FragData[0] = vec4(0.0); \n' + - ' gl_FragData[1] = vec4(1.0); \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; var output = glslModernizeShaderText(noQualifiers, true); var expected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; expect(output).toContain(expected); }); + + it ('creates multiple layout qualifiers under single branch, single condition', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' gl_FragData[1] = vec4(1.0); \n' + + ' #endif //EXAMPLE_BRANCH \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + var expected1 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 1) out vec4 czm_out1;\n#endif'; + expect(output).toContain(expected0); + expect(output).toContain(expected1); + }); + + it ('creates multiple layout qualifiers under multiple branches, single condition (cancels)', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' gl_FragData[1] = vec4(1.0); \n' + + ' #endif //EXAMPLE_BRANCH \n' + + ' #ifndef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' gl_FragData[1] = vec4(1.0); \n' + + ' #endif //!EXAMPLE_BRANCH \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var notExpected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + expect(output).not.toContain(notExpected); + }); + + it ('creates single layout qualifier under multiple branches, multiple conditions (cancels)', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + '#define EXAMPLE_BRANCH1 \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' #endif //EXAMPLE_BRANCH \n' + + ' #ifdef EXAMPLE_BRANCH1 \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' #endif //EXAMPLE_BRANCH1 \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var notExpected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + expect(output).not.toContain(notExpected); + }); + + it ('creates multiple layout qualifier under multiple branches, multiple conditions (cascades)', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + '#define EXAMPLE_BRANCH1 \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' #ifdef EXAMPLE_BRANCH1 \n' + + ' gl_FragData[1] = vec4(0.0); \n' + + ' #endif //EXAMPLE_BRANCH1 \n' + + ' #endif //EXAMPLE_BRANCH \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + var expected1 = ['#ifdef EXAMPLE_BRANCH \n#ifdef EXAMPLE_BRANCH1 \nlayout(location = 0) out vec4 czm_out0;\n#endif', + '#ifdef EXAMPLE_BRANCH1 \n#ifdef EXAMPLE_BRANCH0 \nlayout(location = 0) out vec4 czm_out0;\n#endif']; + var containsExpected0 = ((output.indexOf(expected0[0]) !== -1) && (output.indexOf(expected0[1]) !== -1)); + expect(output).toContain(expected0); + expect(containsExpected0).toBe(true); + }); }); From a1d26cbf03a45901ca81cb56d794fc1e698b2da9 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 17:14:19 -0400 Subject: [PATCH 32/57] Adds even more tests --- Specs/Renderer/GLSLModernizerSpec.js | 125 ++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js index 3e9efc38a91e..cba1d738b0b4 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -78,6 +78,91 @@ defineSuite([ expect(output).toContain(expected1); }); + it ('creates layout qualifier with swizzle', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#extension GL_EXT_draw_buffers : enable \n' + + 'void main() \n' + + '{ \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' gl_FragData[1].xyz = vec3(1.0); \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected0 = 'layout(location = 0) out vec4 czm_out0;'; + var expected1 = 'layout(location = 1) out vec4 czm_out1;'; + expect(output).toContain(expected0); + expect(output).toContain(expected1); + }); + + it ('removes old functions/variables from fragment shader', function() { + var old_fragment = + '#define OUTPUT_DECLARATION \n' + + '#extension GL_EXT_draw_buffers : enable \n' + + 'uniform sampler2D example; \n' + + 'uniform sampler2D exampleCube; \n' + + 'uniform sampler2D example3D; \n' + + 'varying vec2 v_textureCoordinates; \n' + + 'void main() \n' + + '{ \n' + + ' gl_FragData[0] = texture2D(example, v_textureCoordinates); \n' + + ' gl_FragData[1] = textureCube(exampleCube, v_textureCoordinates); \n' + + ' gl_FragData[2] = texture3D(example3D, v_textureCoordinates); \n' + + ' gl_FragDepthEXT = 0.0; \n' + + '} \n'; + + var output = glslModernizeShaderText(old_fragment, true); + + var expectedDepth = 'gl_FragDepth = 0.0;'; + var expectedTexture2D = 'texture(example, v_textureCoordinates);'; + var expectedTextureCube = 'texture(exampleCube, v_textureCoordinates);'; + var expectedTexture3D = 'texture(example3D, v_textureCoordinates);'; + var expectedIn = 'in vec2 v_textureCoordinates;'; + + var notExpectedDepth = 'gl_FragDepthEXT = 0.0;'; + var notExpectedTexture2D = 'texture2D(example, v_textureCoordinates);'; + var notExpectedTextureCube = 'textureCube(exampleCube, v_textureCoordinates);'; + var notExpectedTexture3D = 'texture3D(example3D, v_textureCoordinates);'; + var notExpectedVarying = 'varying vec2 v_textureCoordinates;'; + + expect(output).toContain(expectedDepth); + expect(output).toContain(expectedTexture2D); + expect(output).toContain(expectedTextureCube); + expect(output).toContain(expectedTexture3D); + expect(output).toContain(expectedIn); + + expect(output).not.toContain(notExpectedDepth); + expect(output).not.toContain(notExpectedTexture2D); + expect(output).not.toContain(notExpectedTextureCube); + expect(output).not.toContain(notExpectedTexture3D); + expect(output).not.toContain(notExpectedVarying); + }); + + it ('removes old functions/variables from vertex shader', function() { + var old_vertex = + '#define OUTPUT_DECLARATION \n' + + 'attribute vec4 position; \n' + + 'varying vec4 varyingVariable; \n'; + 'void main() \n' + + '{ \n' + + ' gl_Position = position; \n' + + ' varyingVariable = position.wzyx; \n' + + '} \n'; + + var output = glslModernizeShaderText(old_vertex, false); + + var expectedOut = 'out vec4 varyingVariable;'; + var expectedIn = 'in vec4 position;'; + + var notExpectedAttribute = 'attribute vec4 varyingVariable;'; + var notExpectedVarying = 'varying vec2 varyingVariable;'; + + expect(output).toContain(expectedOut); + expect(output).toContain(expectedIn); + + expect(output).not.toContain(notExpectedAttribute); + expect(output).not.toContain(notExpectedVarying); + }); + it ('creates single layout qualifier under single branch, single condition', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + @@ -150,7 +235,7 @@ defineSuite([ expect(output).not.toContain(notExpected); }); - it ('creates multiple layout qualifier under multiple branches, multiple conditions (cascades)', function() { + it ('creates multiple layout qualifiers under multiple branches, multiple conditions (cascades)', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -160,7 +245,7 @@ defineSuite([ ' #ifdef EXAMPLE_BRANCH \n' + ' gl_FragData[0] = vec4(0.0); \n' + ' #ifdef EXAMPLE_BRANCH1 \n' + - ' gl_FragData[1] = vec4(0.0); \n' + + ' gl_FragData[1] = vec4(1.0); \n' + ' #endif //EXAMPLE_BRANCH1 \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; @@ -172,4 +257,40 @@ defineSuite([ expect(output).toContain(expected0); expect(containsExpected0).toBe(true); }); + + it ('creates single layout qualifier under multiple branches, single condition (else)', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' #else \n' + + ' gl_FragData[0] = vec4(1.0); \n' + + ' #endif //EXAMPLE_BRANCH \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var notExpected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + expect(output).not.toContain(notExpected); + }); + + it ('creates branched layout qualifiers for gl_FragColor and gl_FragData', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + '#define EXAMPLE_BRANCH \n' + + 'void main() \n' + + '{ \n' + + ' #ifdef EXAMPLE_BRANCH \n' + + ' gl_FragData[0] = vec4(0.0); \n' + + ' #else \n' + + ' gl_FragColor = vec4(1.0); \n' + + ' #endif //EXAMPLE_BRANCH \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; + var expected1 = '#ifndef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_fragColor;\n#endif'; + expect(output).toContain(expected0); + expect(output).toContain(expected1); + }); }); From ea6c0317276b895b746d49bfe2a074d0cc69abba Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Thu, 13 Jul 2017 17:27:01 -0400 Subject: [PATCH 33/57] Adds test for poorly named variables --- Specs/Renderer/GLSLModernizerSpec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js index cba1d738b0b4..09935feece1d 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -78,6 +78,28 @@ defineSuite([ expect(output).toContain(expected1); }); + it ('does not create layout qualifier for reserved word lookalike variables functions', function() { + var noQualifiers = + '#define OUTPUT_DECLARATION \n' + + 'uniform sampler2D example; \n' + + 'void main() \n' + + '{ \n' + + ' vec4 gl_FragData_ = vec4(0.0); \n' + + ' vec4 a_gl_FragData = vec4(0.0); \n' + + ' vec2 thisIsNotAGoodNameForAtexture2D = vec2(0.0); \n' + + ' vec4 gl_FragColor = texture2D(example, thisIsNotAGoodNameForAtexture2D); \n' + + '} \n'; + var output = glslModernizeShaderText(noQualifiers, true); + var expectedBadName = 'vec2 thisIsNotAGoodNameForAtexture2D'; + var expectedVariable = 'vec4 a_gl_FragData = vec4(0.0);'; + var expectedTextureCall = 'texture(example, thisIsNotAGoodNameForAtexture2D)'; + var notExpectedLayout = 'layout(location = 0) out czm_out'; + expect(output).toContain(expectedBadName); + expect(output).toContain(expectedVariable); + expect(output).toContain(expectedTextureCall); + expect(output).not.toContain(notExpectedLayout); + }); + it ('creates layout qualifier with swizzle', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + From 201fb993ed6d8216294e029e31d4c59a1c6dd025 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Fri, 14 Jul 2017 09:27:53 -0400 Subject: [PATCH 34/57] Fixes typo --- Specs/Renderer/GLSLModernizerSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js index 09935feece1d..5909bc625c24 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -78,7 +78,7 @@ defineSuite([ expect(output).toContain(expected1); }); - it ('does not create layout qualifier for reserved word lookalike variables functions', function() { + it ('does not create layout qualifier for reserved word lookalike variables', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + 'uniform sampler2D example; \n' + From 0c6ac5b472084f706ed965ebde28b976ce5c0921 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 10:50:27 -0400 Subject: [PATCH 35/57] No longer appending the output declaration define if the context is WebGL 1 --- Source/Renderer/ShaderSource.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 642cdc15e0d3..409dc8097d2e 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -244,7 +244,9 @@ define([ // GLSLModernizer inserts its own layout qualifiers // at this position in the source - result += '#define OUTPUT_DECLARATION\n\n'; + if (context.webgl2) { + result += '#define OUTPUT_DECLARATION\n\n'; + } // append built-ins if (shaderSource.includeBuiltIns) { From 45a8f9944cb3a188813f48ffd0fc22f65aedeefa Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 12:12:53 -0400 Subject: [PATCH 36/57] Fixes eslint issues --- Source/Renderer/GLSLModernizer.js | 125 ++++++++++----------------- Source/Renderer/ShaderCache.js | 2 +- Specs/Renderer/GLSLModernizerSpec.js | 21 +++-- Specs/Renderer/ShaderSourceSpec.js | 2 +- 4 files changed, 60 insertions(+), 90 deletions(-) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/GLSLModernizer.js index 4050067df2b5..99509187174d 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/GLSLModernizer.js @@ -25,7 +25,7 @@ define([ if (source.search(/#version 300 es/g) !== -1) { return source; } - + var outputDeclarationLine = -1; var number; for (number = 0; number < splitSource.length; ++number) { @@ -34,7 +34,7 @@ define([ outputDeclarationLine = number; break; } - }; + } if (outputDeclarationLine === -1) { throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION nor a main function!'); @@ -45,22 +45,22 @@ define([ function replaceInSource(str, replacement, strIsRegex) { var regex = str; if ((strIsRegex === undefined) || strIsRegex === false) { - var regexStr = "(^|[^\\w])(" + str + ")($|[^\\w])"; + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; regex = new RegExp(regexStr, 'g'); } - + for (var number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; if (strIsRegex) { splitSource[number] = line.replace(regex, replacement); } else { - splitSource[number] = line.replace(regex, "$1" + replacement + "$3"); + splitSource[number] = line.replace(regex, '$1' + replacement + '$3'); } } } - + function findInSource(str) { - var regexStr = "(^|[^\\w])(" + str + ")($|[^\\w])"; + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; var regex = new RegExp(regexStr, 'g'); for (var number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; @@ -72,92 +72,63 @@ define([ } function compileSource() { - var wholeSource = ""; + var wholeSource = ''; for (var number = 0; number < splitSource.length; ++number) { - wholeSource += splitSource[number] + "\n"; + wholeSource += splitSource[number] + '\n'; } return wholeSource; } - + function setAdd(variable, set1) { if (set1.indexOf(variable) === -1) - set1.push(variable); - } - - function setUnion(set1, set2) { - for (var a = 0; a < set2.length; ++a) { - setAdd(set2[a], set1); - } + {set1.push(variable);} } function getVariablePreprocessorBranch(variablesThatWeCareAbout) { var variableMap = {}; var negativeMap = {}; - + for (var a = 0; a < variablesThatWeCareAbout.length; ++a) { var variableThatWeCareAbout = variablesThatWeCareAbout[a]; variableMap[variableThatWeCareAbout] = [null]; } - + var stack = []; for (var i = 0; i < splitSource.length; ++i) { var line = splitSource[i]; var hasIF = /(#ifdef|#if)/g.test(line); var hasELSE = /#else/g.test(line); var hasENDIF = /#endif/g.test(line); - + if (hasIF) { stack.push(line); } else if (hasELSE) { var top = stack[stack.length - 1]; - var op = top.replace("ifdef", "ifndef"); + var op = top.replace('ifdef', 'ifndef'); if (/if/g.test(op)) { - op = op.replace(/(#if\s+)(\S*)([^]*)/, "$1!($2)$3"); + op = op.replace(/(#if\s+)(\S*)([^]*)/, '$1!($2)$3'); } stack.pop(); stack.push(op); - + negativeMap[top] = op; negativeMap[op] = top; } else if (hasENDIF) { stack.pop(); } else if (!/layout/g.test(line)) { - for (a = 0; a < variablesThatWeCareAbout.length; ++a) { - var care = variablesThatWeCareAbout[a]; - if (line.indexOf(care) !== -1) { - if (variableMap[care].length === 1 && variableMap[care][0] === null) { - variableMap[care] = stack.slice(); + for (var varIndex = 0; varIndex < variablesThatWeCareAbout.length; ++varIndex) { + var varName = variablesThatWeCareAbout[varIndex]; + if (line.indexOf(varName) !== -1) { + if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { + variableMap[varName] = stack.slice(); } else { - variableMap[care] = variableMap[care].filter(function(x) { return stack.indexOf(x) >= 0; }); + variableMap[varName] = variableMap[varName].filter(function(x) { return stack.indexOf(x) >= 0; }); } } } } } - - // This code is probably no longer needed... it was used to handle - // removing negative conditions - for (var care in variableMap) { - if (variableMap.hasOwnProperty(care)) { - var entry = variableMap[care]; - var toDelete = []; - for (var b = 0; b < entry.length; ++b) { - var item = entry[b]; - if (entry.indexOf(negativeMap[item]) !== -1) { - toDelete.push(item); - toDelete.push(negativeMap[item]); - } - } - - for (var c = 0; c < toDelete.length; ++c) { - var ind = entry.indexOf(toDelete[c]); - if (ind !== -1) { - entry.splice(ind, 1); - } - } - } - } - + for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { if (variableMap.length === 1 && variableMap[0] === null) { @@ -165,51 +136,51 @@ define([ } } } - + return variableMap; } function removeExtension(name) { - var regex = "#extension\\s+GL_" + name + "\\s+:\\s+[a-zA-Z0-9]+\\s*$"; - replaceInSource(new RegExp(regex, "g"), "", true); + var regex = '#extension\\s+GL_' + name + '\\s+:\\s+[a-zA-Z0-9]+\\s*$'; + replaceInSource(new RegExp(regex, 'g'), '', true); } var variableSet = []; for (var i = 0; i < 10; i++) { - var fragDataString = "gl_FragData\\[" + i + "\\]"; + var fragDataString = 'gl_FragData\\[' + i + '\\]'; var newOutput = 'czm_out' + i; - var regex = new RegExp(fragDataString, "g"); + var regex = new RegExp(fragDataString, 'g'); if (regex.test(source)) { setAdd(newOutput, variableSet); replaceInSource(fragDataString, newOutput); - splitSource.splice(outputDeclarationLine, 0, "layout(location = " + i + ") out vec4 " + newOutput + ";"); + splitSource.splice(outputDeclarationLine, 0, 'layout(location = ' + i + ') out vec4 ' + newOutput + ';'); outputDeclarationLine += 1; } } - var czmFragColor = "czm_fragColor"; - if (findInSource("gl_FragColor")) { + var czmFragColor = 'czm_fragColor'; + if (findInSource('gl_FragColor')) { setAdd(czmFragColor, variableSet); - replaceInSource("gl_FragColor", czmFragColor); - splitSource.splice(outputDeclarationLine, 0, "layout(location = 0) out vec4 czm_fragColor;"); + replaceInSource('gl_FragColor', czmFragColor); + splitSource.splice(outputDeclarationLine, 0, 'layout(location = 0) out vec4 czm_fragColor;'); outputDeclarationLine += 1; } - + var variableMap = getVariablePreprocessorBranch(variableSet); var lineAdds = {}; for (var c = 0; c < splitSource.length; c++) { var l = splitSource[c]; for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { - var matchVar = new RegExp("(layout)[^]+(out)[^]+(" + care + ")[^]+", "g"); + var matchVar = new RegExp('(layout)[^]+(out)[^]+(' + care + ')[^]+', 'g'); if (matchVar.exec(l) !== null) { lineAdds[l] = care; } } } } - + for (var layoutDeclaration in lineAdds) { if (lineAdds.hasOwnProperty(layoutDeclaration)) { var variableName = lineAdds[layoutDeclaration]; @@ -222,12 +193,12 @@ define([ } lineNumber += depth + 1; for (d = depth - 1; d >= 0; d--) { - splitSource.splice(lineNumber, 0, "#endif //" + entry[d]); + splitSource.splice(lineNumber, 0, '#endif //' + entry[d]); } } } - var versionThree = "#version 300 es"; + var versionThree = '#version 300 es'; var foundVersion = false; for (number = 0; number < splitSource.length; number++) { if (/#version/.test(splitSource[number])) { @@ -240,19 +211,19 @@ define([ splitSource.splice(0, 0, versionThree); } - removeExtension("EXT_draw_buffers"); - removeExtension("EXT_frag_depth"); + removeExtension('EXT_draw_buffers'); + removeExtension('EXT_frag_depth'); - replaceInSource("texture2D", "texture"); - replaceInSource("texture3D", "texture"); - replaceInSource("textureCube", "texture"); - replaceInSource("gl_FragDepthEXT", "gl_FragDepth"); + replaceInSource('texture2D', 'texture'); + replaceInSource('texture3D', 'texture'); + replaceInSource('textureCube', 'texture'); + replaceInSource('gl_FragDepthEXT', 'gl_FragDepth'); if (isFragmentShader) { - replaceInSource("varying", "in"); + replaceInSource('varying', 'in'); } else { - replaceInSource("attribute", "in"); - replaceInSource("varying", "out"); + replaceInSource('attribute', 'in'); + replaceInSource('varying', 'out'); } return compileSource(); diff --git a/Source/Renderer/ShaderCache.js b/Source/Renderer/ShaderCache.js index 70092c6954f0..a2a9e86d7358 100644 --- a/Source/Renderer/ShaderCache.js +++ b/Source/Renderer/ShaderCache.js @@ -170,7 +170,7 @@ define([ } var context = this._context; - + var vertexShaderText = vertexShaderSource.createCombinedVertexShader(context); var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(context); diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/GLSLModernizerSpec.js index 5909bc625c24..6d3507e257fd 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/GLSLModernizerSpec.js @@ -129,11 +129,11 @@ defineSuite([ ' gl_FragData[0] = texture2D(example, v_textureCoordinates); \n' + ' gl_FragData[1] = textureCube(exampleCube, v_textureCoordinates); \n' + ' gl_FragData[2] = texture3D(example3D, v_textureCoordinates); \n' + - ' gl_FragDepthEXT = 0.0; \n' + + ' gl_FragDepthEXT = 0.0; \n' + '} \n'; - + var output = glslModernizeShaderText(old_fragment, true); - + var expectedDepth = 'gl_FragDepth = 0.0;'; var expectedTexture2D = 'texture(example, v_textureCoordinates);'; var expectedTextureCube = 'texture(exampleCube, v_textureCoordinates);'; @@ -145,7 +145,7 @@ defineSuite([ var notExpectedTextureCube = 'textureCube(exampleCube, v_textureCoordinates);'; var notExpectedTexture3D = 'texture3D(example3D, v_textureCoordinates);'; var notExpectedVarying = 'varying vec2 v_textureCoordinates;'; - + expect(output).toContain(expectedDepth); expect(output).toContain(expectedTexture2D); expect(output).toContain(expectedTextureCube); @@ -163,21 +163,21 @@ defineSuite([ var old_vertex = '#define OUTPUT_DECLARATION \n' + 'attribute vec4 position; \n' + - 'varying vec4 varyingVariable; \n'; + 'varying vec4 varyingVariable; \n' + 'void main() \n' + '{ \n' + ' gl_Position = position; \n' + ' varyingVariable = position.wzyx; \n' + '} \n'; - + var output = glslModernizeShaderText(old_vertex, false); - + var expectedOut = 'out vec4 varyingVariable;'; var expectedIn = 'in vec4 position;'; var notExpectedAttribute = 'attribute vec4 varyingVariable;'; var notExpectedVarying = 'varying vec2 varyingVariable;'; - + expect(output).toContain(expectedOut); expect(output).toContain(expectedIn); @@ -273,9 +273,8 @@ defineSuite([ '} \n'; var output = glslModernizeShaderText(noQualifiers, true); var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; - var expected1 = ['#ifdef EXAMPLE_BRANCH \n#ifdef EXAMPLE_BRANCH1 \nlayout(location = 0) out vec4 czm_out0;\n#endif', - '#ifdef EXAMPLE_BRANCH1 \n#ifdef EXAMPLE_BRANCH0 \nlayout(location = 0) out vec4 czm_out0;\n#endif']; - var containsExpected0 = ((output.indexOf(expected0[0]) !== -1) && (output.indexOf(expected0[1]) !== -1)); + var expected1 = /#ifdef (EXAMPLE_BRANCH|EXAMPLE_BRANCH1)\s*\n\s*#ifdef (EXAMPLE_BRANCH1|EXAMPLE_BRANCH)\s*\n\s*layout\(location = 1\) out vec4 czm_out1;/g; + var containsExpected0 = expected1.test(output); expect(output).toContain(expected0); expect(containsExpected0).toBe(true); }); diff --git a/Specs/Renderer/ShaderSourceSpec.js b/Specs/Renderer/ShaderSourceSpec.js index 461819a070f1..80b4f4fbe9c4 100644 --- a/Specs/Renderer/ShaderSourceSpec.js +++ b/Specs/Renderer/ShaderSourceSpec.js @@ -7,7 +7,7 @@ defineSuite([ var mockContext = { webgl2 : false }; - + it('combines #defines', function() { var source = new ShaderSource({ defines : ['A', 'B', ''] From 020bba76a07dfb408a49145c06ce6c69868eb24c Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 13:37:01 -0400 Subject: [PATCH 37/57] Fixes problem where we were passing context.webgl2 instead of context into ShaderSource.createCombinedFragmentShader --- Specs/Renderer/ShaderProgramSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/Renderer/ShaderProgramSpec.js b/Specs/Renderer/ShaderProgramSpec.js index dc5a9b5e190d..0fe33e0acc0b 100644 --- a/Specs/Renderer/ShaderProgramSpec.js +++ b/Specs/Renderer/ShaderProgramSpec.js @@ -71,7 +71,7 @@ defineSuite([ var expectedFSText = new ShaderSource({ sources : [fs] - }).createCombinedFragmentShader(context.webgl2); + }).createCombinedFragmentShader(context); expect(sp._fragmentShaderText).toEqual(expectedFSText); }); From fd633e93a754ebcbd9e8eef037e8e3e67035a60d Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 13:43:07 -0400 Subject: [PATCH 38/57] Renames GLSLModernizer to ModernizeShader --- .../{GLSLModernizer.js => ModernizeShader.js} | 4 +- Source/Renderer/ShaderSource.js | 6 +-- ...dernizerSpec.js => ModernizeShaderSpec.js} | 38 +++++++++---------- 3 files changed, 24 insertions(+), 24 deletions(-) rename Source/Renderer/{GLSLModernizer.js => ModernizeShader.js} (98%) rename Specs/Renderer/{GLSLModernizerSpec.js => ModernizeShaderSpec.js} (91%) diff --git a/Source/Renderer/GLSLModernizer.js b/Source/Renderer/ModernizeShader.js similarity index 98% rename from Source/Renderer/GLSLModernizer.js rename to Source/Renderer/ModernizeShader.js index 99509187174d..1d019c92b18a 100644 --- a/Source/Renderer/GLSLModernizer.js +++ b/Source/Renderer/ModernizeShader.js @@ -18,7 +18,7 @@ define([ * * @private */ - function glslModernizeShaderText(source, isFragmentShader) { + function modernizeShader(source, isFragmentShader) { var outputDeclarationRegex = /#define OUTPUT_DECLARATION/; var splitSource = source.split('\n'); @@ -229,5 +229,5 @@ define([ return compileSource(); } - return glslModernizeShaderText; + return modernizeShader; }); diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 409dc8097d2e..da96cbdf4526 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -2,14 +2,14 @@ define([ '../Core/defaultValue', '../Core/defined', '../Core/DeveloperError', - '../Renderer/GLSLModernizer', + '../Renderer/ModernizeShader', '../Shaders/Builtin/CzmBuiltins', './AutomaticUniforms' ], function( defaultValue, defined, DeveloperError, - glslModernizeShaderText, + modernizeShader, CzmBuiltins, AutomaticUniforms) { 'use strict'; @@ -261,7 +261,7 @@ define([ // modernize the source if (context.webgl2) { - result = glslModernizeShaderText(result, isFragmentShader, true); + result = modernizeShader(result, isFragmentShader, true); } return result; diff --git a/Specs/Renderer/GLSLModernizerSpec.js b/Specs/Renderer/ModernizeShaderSpec.js similarity index 91% rename from Specs/Renderer/GLSLModernizerSpec.js rename to Specs/Renderer/ModernizeShaderSpec.js index 6d3507e257fd..89091f73c16a 100644 --- a/Specs/Renderer/GLSLModernizerSpec.js +++ b/Specs/Renderer/ModernizeShaderSpec.js @@ -1,7 +1,7 @@ defineSuite([ - 'Renderer/GLSLModernizer' + 'Renderer/ModernizeShader' ], function( - glslModernizeShaderText) { + modernizeShader) { 'use strict'; it ('adds version string', function() { @@ -10,7 +10,7 @@ defineSuite([ 'void main() \n' + '{ \n' + '} \n'; - var output = glslModernizeShaderText(simple, true); + var output = modernizeShader(simple, true); var expected = '#version 300 es'; expect(output.split('\n')[0]).toEqual(expected); }); @@ -22,7 +22,7 @@ defineSuite([ 'void main() \n' + '{ \n' + '} \n'; - var output = glslModernizeShaderText(extensions, true); + var output = modernizeShader(extensions, true); var notExpected = '#extension GL_EXT_draw_buffers : enable \n'; expect(output).not.toContain(notExpected); }); @@ -33,7 +33,7 @@ defineSuite([ '{ \n' + '} \n'; var runFunc = function() { - glslModernizeShaderText(noOutputDeclaration, true); + modernizeShader(noOutputDeclaration, true); }; expect(runFunc).toThrow(); }); @@ -45,7 +45,7 @@ defineSuite([ '{ \n' + ' gl_FragColor = vec4(0.0); \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected = 'layout(location = 0) out vec4 czm_fragColor;'; expect(output).toContain(expected); }); @@ -57,7 +57,7 @@ defineSuite([ '{ \n' + ' gl_FragData[0] = vec4(0.0); \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected = 'layout(location = 0) out vec4 czm_out0;'; expect(output).toContain(expected); }); @@ -71,7 +71,7 @@ defineSuite([ ' gl_FragData[0] = vec4(0.0); \n' + ' gl_FragData[1] = vec4(1.0); \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected0 = 'layout(location = 0) out vec4 czm_out0;'; var expected1 = 'layout(location = 1) out vec4 czm_out1;'; expect(output).toContain(expected0); @@ -89,7 +89,7 @@ defineSuite([ ' vec2 thisIsNotAGoodNameForAtexture2D = vec2(0.0); \n' + ' vec4 gl_FragColor = texture2D(example, thisIsNotAGoodNameForAtexture2D); \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expectedBadName = 'vec2 thisIsNotAGoodNameForAtexture2D'; var expectedVariable = 'vec4 a_gl_FragData = vec4(0.0);'; var expectedTextureCall = 'texture(example, thisIsNotAGoodNameForAtexture2D)'; @@ -109,7 +109,7 @@ defineSuite([ ' gl_FragData[0] = vec4(0.0); \n' + ' gl_FragData[1].xyz = vec3(1.0); \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected0 = 'layout(location = 0) out vec4 czm_out0;'; var expected1 = 'layout(location = 1) out vec4 czm_out1;'; expect(output).toContain(expected0); @@ -132,7 +132,7 @@ defineSuite([ ' gl_FragDepthEXT = 0.0; \n' + '} \n'; - var output = glslModernizeShaderText(old_fragment, true); + var output = modernizeShader(old_fragment, true); var expectedDepth = 'gl_FragDepth = 0.0;'; var expectedTexture2D = 'texture(example, v_textureCoordinates);'; @@ -170,7 +170,7 @@ defineSuite([ ' varyingVariable = position.wzyx; \n' + '} \n'; - var output = glslModernizeShaderText(old_vertex, false); + var output = modernizeShader(old_vertex, false); var expectedOut = 'out vec4 varyingVariable;'; var expectedIn = 'in vec4 position;'; @@ -195,7 +195,7 @@ defineSuite([ ' gl_FragData[0] = vec4(0.0); \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; expect(output).toContain(expected); }); @@ -211,7 +211,7 @@ defineSuite([ ' gl_FragData[1] = vec4(1.0); \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; var expected1 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 1) out vec4 czm_out1;\n#endif'; expect(output).toContain(expected0); @@ -233,7 +233,7 @@ defineSuite([ ' gl_FragData[1] = vec4(1.0); \n' + ' #endif //!EXAMPLE_BRANCH \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var notExpected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; expect(output).not.toContain(notExpected); }); @@ -252,7 +252,7 @@ defineSuite([ ' gl_FragData[0] = vec4(0.0); \n' + ' #endif //EXAMPLE_BRANCH1 \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var notExpected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; expect(output).not.toContain(notExpected); }); @@ -271,7 +271,7 @@ defineSuite([ ' #endif //EXAMPLE_BRANCH1 \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; var expected1 = /#ifdef (EXAMPLE_BRANCH|EXAMPLE_BRANCH1)\s*\n\s*#ifdef (EXAMPLE_BRANCH1|EXAMPLE_BRANCH)\s*\n\s*layout\(location = 1\) out vec4 czm_out1;/g; var containsExpected0 = expected1.test(output); @@ -291,7 +291,7 @@ defineSuite([ ' gl_FragData[0] = vec4(1.0); \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var notExpected = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; expect(output).not.toContain(notExpected); }); @@ -308,7 +308,7 @@ defineSuite([ ' gl_FragColor = vec4(1.0); \n' + ' #endif //EXAMPLE_BRANCH \n' + '} \n'; - var output = glslModernizeShaderText(noQualifiers, true); + var output = modernizeShader(noQualifiers, true); var expected0 = '#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif'; var expected1 = '#ifndef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_fragColor;\n#endif'; expect(output).toContain(expected0); From 1686573ad9c2cc7fbcd7304354b2197f46d99cd7 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 13:46:49 -0400 Subject: [PATCH 39/57] Now using .test() instead of .search and fixes a typo --- Source/Renderer/ModernizeShader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index 1d019c92b18a..5aff694364a2 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -22,7 +22,7 @@ define([ var outputDeclarationRegex = /#define OUTPUT_DECLARATION/; var splitSource = source.split('\n'); - if (source.search(/#version 300 es/g) !== -1) { + if (/#version 300 es/g.test(source) !== -1) { return source; } @@ -37,7 +37,7 @@ define([ } if (outputDeclarationLine === -1) { - throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION nor a main function!'); + throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION!'); } // Note that this fails if your string looks like From d9167a2be170c8f4156f279f1903ce5bb554ab5b Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 13:56:37 -0400 Subject: [PATCH 40/57] Splits replace into two different functions --- Source/Renderer/ModernizeShader.js | 42 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index 5aff694364a2..77d4d38d9d08 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -22,7 +22,7 @@ define([ var outputDeclarationRegex = /#define OUTPUT_DECLARATION/; var splitSource = source.split('\n'); - if (/#version 300 es/g.test(source) !== -1) { + if (/#version 300 es/g.test(source)) { return source; } @@ -42,20 +42,20 @@ define([ // Note that this fails if your string looks like // searchString[singleCharacter]searchString - function replaceInSource(str, replacement, strIsRegex) { - var regex = str; - if ((strIsRegex === undefined) || strIsRegex === false) { - var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; - regex = new RegExp(regexStr, 'g'); + function replaceInSourceString(str, replacement) { + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; + var regex = new RegExp(regexStr, 'g'); + + for (var number = 0; number < splitSource.length; ++number) { + var line = splitSource[number]; + splitSource[number] = line.replace(regex, '$1' + replacement + '$3'); } + } + function replaceInSourceRegex(regex, replacement) { for (var number = 0; number < splitSource.length; ++number) { var line = splitSource[number]; - if (strIsRegex) { - splitSource[number] = line.replace(regex, replacement); - } else { - splitSource[number] = line.replace(regex, '$1' + replacement + '$3'); - } + splitSource[number] = line.replace(regex, replacement); } } @@ -142,7 +142,7 @@ define([ function removeExtension(name) { var regex = '#extension\\s+GL_' + name + '\\s+:\\s+[a-zA-Z0-9]+\\s*$'; - replaceInSource(new RegExp(regex, 'g'), '', true); + replaceInSourceRegex(new RegExp(regex, 'g'), '', true); } var variableSet = []; @@ -153,7 +153,7 @@ define([ var regex = new RegExp(fragDataString, 'g'); if (regex.test(source)) { setAdd(newOutput, variableSet); - replaceInSource(fragDataString, newOutput); + replaceInSourceString(fragDataString, newOutput); splitSource.splice(outputDeclarationLine, 0, 'layout(location = ' + i + ') out vec4 ' + newOutput + ';'); outputDeclarationLine += 1; } @@ -162,7 +162,7 @@ define([ var czmFragColor = 'czm_fragColor'; if (findInSource('gl_FragColor')) { setAdd(czmFragColor, variableSet); - replaceInSource('gl_FragColor', czmFragColor); + replaceInSourceString('gl_FragColor', czmFragColor); splitSource.splice(outputDeclarationLine, 0, 'layout(location = 0) out vec4 czm_fragColor;'); outputDeclarationLine += 1; } @@ -214,16 +214,16 @@ define([ removeExtension('EXT_draw_buffers'); removeExtension('EXT_frag_depth'); - replaceInSource('texture2D', 'texture'); - replaceInSource('texture3D', 'texture'); - replaceInSource('textureCube', 'texture'); - replaceInSource('gl_FragDepthEXT', 'gl_FragDepth'); + replaceInSourceString('texture2D', 'texture'); + replaceInSourceString('texture3D', 'texture'); + replaceInSourceString('textureCube', 'texture'); + replaceInSourceString('gl_FragDepthEXT', 'gl_FragDepth'); if (isFragmentShader) { - replaceInSource('varying', 'in'); + replaceInSourceString('varying', 'in'); } else { - replaceInSource('attribute', 'in'); - replaceInSource('varying', 'out'); + replaceInSourceString('attribute', 'in'); + replaceInSourceString('varying', 'out'); } return compileSource(); From 9403d8fb229cf03a97accd754087ad58fbef6767 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:01:21 -0400 Subject: [PATCH 41/57] All the functions are now external --- Source/Renderer/ModernizeShader.js | 241 +++++++++++++++-------------- 1 file changed, 122 insertions(+), 119 deletions(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index 77d4d38d9d08..8f5e231294bf 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -6,6 +6,114 @@ define([ DeveloperError) { 'use strict'; + // Note that this fails if your string looks like + // searchString[singleCharacter]searchString + function replaceInSourceString(str, replacement, splitSource) { + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; + var regex = new RegExp(regexStr, 'g'); + + for (var number = 0; number < splitSource.length; ++number) { + var line = splitSource[number]; + splitSource[number] = line.replace(regex, '$1' + replacement + '$3'); + } + } + + function replaceInSourceRegex(regex, replacement, splitSource) { + for (var number = 0; number < splitSource.length; ++number) { + var line = splitSource[number]; + splitSource[number] = line.replace(regex, replacement); + } + } + + function findInSource(str, splitSource) { + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; + var regex = new RegExp(regexStr, 'g'); + for (var number = 0; number < splitSource.length; ++number) { + var line = splitSource[number]; + if (regex.test(line)) { + return true; + } + } + return false; + } + + function compileSource(splitSource) { + var wholeSource = ''; + for (var number = 0; number < splitSource.length; ++number) { + wholeSource += splitSource[number] + '\n'; + } + return wholeSource; + } + + function setAdd(variable, set1) { + if (set1.indexOf(variable) === -1) { + set1.push(variable); + } + } + + function getVariablePreprocessorBranch(variablesThatWeCareAbout, splitSource) { + var variableMap = {}; + var negativeMap = {}; + + for (var a = 0; a < variablesThatWeCareAbout.length; ++a) { + var variableThatWeCareAbout = variablesThatWeCareAbout[a]; + variableMap[variableThatWeCareAbout] = [null]; + } + + var stack = []; + for (var i = 0; i < splitSource.length; ++i) { + var line = splitSource[i]; + var hasIF = /(#ifdef|#if)/g.test(line); + var hasELSE = /#else/g.test(line); + var hasENDIF = /#endif/g.test(line); + + if (hasIF) { + stack.push(line); + } else if (hasELSE) { + var top = stack[stack.length - 1]; + var op = top.replace('ifdef', 'ifndef'); + if (/if/g.test(op)) { + op = op.replace(/(#if\s+)(\S*)([^]*)/, '$1!($2)$3'); + } + stack.pop(); + stack.push(op); + + negativeMap[top] = op; + negativeMap[op] = top; + } else if (hasENDIF) { + stack.pop(); + } else if (!/layout/g.test(line)) { + for (var varIndex = 0; varIndex < variablesThatWeCareAbout.length; ++varIndex) { + var varName = variablesThatWeCareAbout[varIndex]; + if (line.indexOf(varName) !== -1) { + if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { + variableMap[varName] = stack.slice(); + } else { + variableMap[varName] = variableMap[varName].filter(function(x) { + return stack.indexOf(x) >= 0; + }); + } + } + } + } + } + + for (var care in variableMap) { + if (variableMap.hasOwnProperty(care)) { + if (variableMap.length === 1 && variableMap[0] === null) { + variableMap.splice(0, 1); + } + } + } + + return variableMap; + } + + function removeExtension(name, splitSource) { + var regex = '#extension\\s+GL_' + name + '\\s+:\\s+[a-zA-Z0-9]+\\s*$'; + replaceInSourceRegex(new RegExp(regex, 'g'), '', splitSource); + } + /** * A function to port GLSL shaders from GLSL ES 1.00 to GLSL ES 3.00 * @@ -40,111 +148,6 @@ define([ throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION!'); } - // Note that this fails if your string looks like - // searchString[singleCharacter]searchString - function replaceInSourceString(str, replacement) { - var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; - var regex = new RegExp(regexStr, 'g'); - - for (var number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - splitSource[number] = line.replace(regex, '$1' + replacement + '$3'); - } - } - - function replaceInSourceRegex(regex, replacement) { - for (var number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - splitSource[number] = line.replace(regex, replacement); - } - } - - function findInSource(str) { - var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; - var regex = new RegExp(regexStr, 'g'); - for (var number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - if (regex.test(line)) { - return true; - } - } - return false; - } - - function compileSource() { - var wholeSource = ''; - for (var number = 0; number < splitSource.length; ++number) { - wholeSource += splitSource[number] + '\n'; - } - return wholeSource; - } - - function setAdd(variable, set1) { - if (set1.indexOf(variable) === -1) - {set1.push(variable);} - } - - function getVariablePreprocessorBranch(variablesThatWeCareAbout) { - var variableMap = {}; - var negativeMap = {}; - - for (var a = 0; a < variablesThatWeCareAbout.length; ++a) { - var variableThatWeCareAbout = variablesThatWeCareAbout[a]; - variableMap[variableThatWeCareAbout] = [null]; - } - - var stack = []; - for (var i = 0; i < splitSource.length; ++i) { - var line = splitSource[i]; - var hasIF = /(#ifdef|#if)/g.test(line); - var hasELSE = /#else/g.test(line); - var hasENDIF = /#endif/g.test(line); - - if (hasIF) { - stack.push(line); - } else if (hasELSE) { - var top = stack[stack.length - 1]; - var op = top.replace('ifdef', 'ifndef'); - if (/if/g.test(op)) { - op = op.replace(/(#if\s+)(\S*)([^]*)/, '$1!($2)$3'); - } - stack.pop(); - stack.push(op); - - negativeMap[top] = op; - negativeMap[op] = top; - } else if (hasENDIF) { - stack.pop(); - } else if (!/layout/g.test(line)) { - for (var varIndex = 0; varIndex < variablesThatWeCareAbout.length; ++varIndex) { - var varName = variablesThatWeCareAbout[varIndex]; - if (line.indexOf(varName) !== -1) { - if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { - variableMap[varName] = stack.slice(); - } else { - variableMap[varName] = variableMap[varName].filter(function(x) { return stack.indexOf(x) >= 0; }); - } - } - } - } - } - - for (var care in variableMap) { - if (variableMap.hasOwnProperty(care)) { - if (variableMap.length === 1 && variableMap[0] === null) { - variableMap.splice(0, 1); - } - } - } - - return variableMap; - } - - function removeExtension(name) { - var regex = '#extension\\s+GL_' + name + '\\s+:\\s+[a-zA-Z0-9]+\\s*$'; - replaceInSourceRegex(new RegExp(regex, 'g'), '', true); - } - var variableSet = []; for (var i = 0; i < 10; i++) { @@ -153,21 +156,21 @@ define([ var regex = new RegExp(fragDataString, 'g'); if (regex.test(source)) { setAdd(newOutput, variableSet); - replaceInSourceString(fragDataString, newOutput); + replaceInSourceString(fragDataString, newOutput, splitSource); splitSource.splice(outputDeclarationLine, 0, 'layout(location = ' + i + ') out vec4 ' + newOutput + ';'); outputDeclarationLine += 1; } } var czmFragColor = 'czm_fragColor'; - if (findInSource('gl_FragColor')) { + if (findInSource('gl_FragColor', splitSource)) { setAdd(czmFragColor, variableSet); - replaceInSourceString('gl_FragColor', czmFragColor); + replaceInSourceString('gl_FragColor', czmFragColor, splitSource); splitSource.splice(outputDeclarationLine, 0, 'layout(location = 0) out vec4 czm_fragColor;'); outputDeclarationLine += 1; } - var variableMap = getVariablePreprocessorBranch(variableSet); + var variableMap = getVariablePreprocessorBranch(variableSet, splitSource); var lineAdds = {}; for (var c = 0; c < splitSource.length; c++) { var l = splitSource[c]; @@ -211,22 +214,22 @@ define([ splitSource.splice(0, 0, versionThree); } - removeExtension('EXT_draw_buffers'); - removeExtension('EXT_frag_depth'); + removeExtension('EXT_draw_buffers', splitSource); + removeExtension('EXT_frag_depth', splitSource); - replaceInSourceString('texture2D', 'texture'); - replaceInSourceString('texture3D', 'texture'); - replaceInSourceString('textureCube', 'texture'); - replaceInSourceString('gl_FragDepthEXT', 'gl_FragDepth'); + replaceInSourceString('texture2D', 'texture', splitSource); + replaceInSourceString('texture3D', 'texture', splitSource); + replaceInSourceString('textureCube', 'texture', splitSource); + replaceInSourceString('gl_FragDepthEXT', 'gl_FragDepth', splitSource); if (isFragmentShader) { - replaceInSourceString('varying', 'in'); + replaceInSourceString('varying', 'in', splitSource); } else { - replaceInSourceString('attribute', 'in'); - replaceInSourceString('varying', 'out'); + replaceInSourceString('attribute', 'in', splitSource); + replaceInSourceString('varying', 'out', splitSource); } - return compileSource(); + return compileSource(splitSource); } return modernizeShader; From a37d25f26ddd6b4b95317ac5cfaf220b23dfd7eb Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:02:54 -0400 Subject: [PATCH 42/57] Renames length variable --- Source/Renderer/ModernizeShader.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index 8f5e231294bf..26fbed91fed9 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -55,7 +55,8 @@ define([ var variableMap = {}; var negativeMap = {}; - for (var a = 0; a < variablesThatWeCareAbout.length; ++a) { + var numVariablesWeCareAbout = variablesThatWeCareAbout.length; + for (var a = 0; a < numVariablesWeCareAbout; ++a) { var variableThatWeCareAbout = variablesThatWeCareAbout[a]; variableMap[variableThatWeCareAbout] = [null]; } @@ -83,7 +84,7 @@ define([ } else if (hasENDIF) { stack.pop(); } else if (!/layout/g.test(line)) { - for (var varIndex = 0; varIndex < variablesThatWeCareAbout.length; ++varIndex) { + for (var varIndex = 0; varIndex < numVariablesWeCareAbout; ++varIndex) { var varName = variablesThatWeCareAbout[varIndex]; if (line.indexOf(varName) !== -1) { if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { From 361691e28f25f75f8c79bd5d16ea93876f9d0447 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:04:05 -0400 Subject: [PATCH 43/57] Removes negativeMap --- Source/Renderer/ModernizeShader.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index 26fbed91fed9..a050ef5614d7 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -53,7 +53,6 @@ define([ function getVariablePreprocessorBranch(variablesThatWeCareAbout, splitSource) { var variableMap = {}; - var negativeMap = {}; var numVariablesWeCareAbout = variablesThatWeCareAbout.length; for (var a = 0; a < numVariablesWeCareAbout; ++a) { @@ -78,9 +77,6 @@ define([ } stack.pop(); stack.push(op); - - negativeMap[top] = op; - negativeMap[op] = top; } else if (hasENDIF) { stack.pop(); } else if (!/layout/g.test(line)) { From 3c80a138321119dcb5d66386e0a4ac04c620f1ae Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:05:18 -0400 Subject: [PATCH 44/57] Renames variableSet to outputVariables --- Source/Renderer/ModernizeShader.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index a050ef5614d7..40c3ffaa8c52 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -145,14 +145,14 @@ define([ throw new DeveloperError('Could not find a #define OUTPUT_DECLARATION!'); } - var variableSet = []; + var outputVariables = []; for (var i = 0; i < 10; i++) { var fragDataString = 'gl_FragData\\[' + i + '\\]'; var newOutput = 'czm_out' + i; var regex = new RegExp(fragDataString, 'g'); if (regex.test(source)) { - setAdd(newOutput, variableSet); + setAdd(newOutput, outputVariables); replaceInSourceString(fragDataString, newOutput, splitSource); splitSource.splice(outputDeclarationLine, 0, 'layout(location = ' + i + ') out vec4 ' + newOutput + ';'); outputDeclarationLine += 1; @@ -161,13 +161,13 @@ define([ var czmFragColor = 'czm_fragColor'; if (findInSource('gl_FragColor', splitSource)) { - setAdd(czmFragColor, variableSet); + setAdd(czmFragColor, outputVariables); replaceInSourceString('gl_FragColor', czmFragColor, splitSource); splitSource.splice(outputDeclarationLine, 0, 'layout(location = 0) out vec4 czm_fragColor;'); outputDeclarationLine += 1; } - var variableMap = getVariablePreprocessorBranch(variableSet, splitSource); + var variableMap = getVariablePreprocessorBranch(outputVariables, splitSource); var lineAdds = {}; for (var c = 0; c < splitSource.length; c++) { var l = splitSource[c]; From 59b140265bd8376bd3be9beaf64a232295226add Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:06:14 -0400 Subject: [PATCH 45/57] Replaces RegExp.exec with RegExp.test --- Source/Renderer/ModernizeShader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/ModernizeShader.js index 40c3ffaa8c52..bc078e9d5112 100644 --- a/Source/Renderer/ModernizeShader.js +++ b/Source/Renderer/ModernizeShader.js @@ -174,7 +174,7 @@ define([ for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { var matchVar = new RegExp('(layout)[^]+(out)[^]+(' + care + ')[^]+', 'g'); - if (matchVar.exec(l) !== null) { + if (matchVar.test(l)) { lineAdds[l] = care; } } From 0addb522909dcb8ba9fc6dae1163eafca0021dc3 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:07:11 -0400 Subject: [PATCH 46/57] Replaces all the "it (" with "it(" --- Specs/Renderer/ModernizeShaderSpec.js | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Specs/Renderer/ModernizeShaderSpec.js b/Specs/Renderer/ModernizeShaderSpec.js index 89091f73c16a..5e02be5d264b 100644 --- a/Specs/Renderer/ModernizeShaderSpec.js +++ b/Specs/Renderer/ModernizeShaderSpec.js @@ -4,7 +4,7 @@ defineSuite([ modernizeShader) { 'use strict'; - it ('adds version string', function() { + it('adds version string', function() { var simple = '#define OUTPUT_DECLARATION \n' + 'void main() \n' + @@ -15,7 +15,7 @@ defineSuite([ expect(output.split('\n')[0]).toEqual(expected); }); - it ('removes extensions', function() { + it('removes extensions', function() { var extensions = '#define OUTPUT_DECLARATION \n' + '#extension GL_EXT_draw_buffers : enable \n' + @@ -27,7 +27,7 @@ defineSuite([ expect(output).not.toContain(notExpected); }); - it ('throws exception if no output declaration', function() { + it('throws exception if no output declaration', function() { var noOutputDeclaration = 'void main() \n' + '{ \n' + @@ -38,7 +38,7 @@ defineSuite([ expect(runFunc).toThrow(); }); - it ('creates layout qualifier for gl_FragColor', function() { + it('creates layout qualifier for gl_FragColor', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + 'void main() \n' + @@ -50,7 +50,7 @@ defineSuite([ expect(output).toContain(expected); }); - it ('creates layout qualifier for gl_FragData', function() { + it('creates layout qualifier for gl_FragData', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + 'void main() \n' + @@ -62,7 +62,7 @@ defineSuite([ expect(output).toContain(expected); }); - it ('creates layout qualifier for MRT', function() { + it('creates layout qualifier for MRT', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#extension GL_EXT_draw_buffers : enable \n' + @@ -78,7 +78,7 @@ defineSuite([ expect(output).toContain(expected1); }); - it ('does not create layout qualifier for reserved word lookalike variables', function() { + it('does not create layout qualifier for reserved word lookalike variables', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + 'uniform sampler2D example; \n' + @@ -100,7 +100,7 @@ defineSuite([ expect(output).not.toContain(notExpectedLayout); }); - it ('creates layout qualifier with swizzle', function() { + it('creates layout qualifier with swizzle', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#extension GL_EXT_draw_buffers : enable \n' + @@ -116,7 +116,7 @@ defineSuite([ expect(output).toContain(expected1); }); - it ('removes old functions/variables from fragment shader', function() { + it('removes old functions/variables from fragment shader', function() { var old_fragment = '#define OUTPUT_DECLARATION \n' + '#extension GL_EXT_draw_buffers : enable \n' + @@ -159,7 +159,7 @@ defineSuite([ expect(output).not.toContain(notExpectedVarying); }); - it ('removes old functions/variables from vertex shader', function() { + it('removes old functions/variables from vertex shader', function() { var old_vertex = '#define OUTPUT_DECLARATION \n' + 'attribute vec4 position; \n' + @@ -185,7 +185,7 @@ defineSuite([ expect(output).not.toContain(notExpectedVarying); }); - it ('creates single layout qualifier under single branch, single condition', function() { + it('creates single layout qualifier under single branch, single condition', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -200,7 +200,7 @@ defineSuite([ expect(output).toContain(expected); }); - it ('creates multiple layout qualifiers under single branch, single condition', function() { + it('creates multiple layout qualifiers under single branch, single condition', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -218,7 +218,7 @@ defineSuite([ expect(output).toContain(expected1); }); - it ('creates multiple layout qualifiers under multiple branches, single condition (cancels)', function() { + it('creates multiple layout qualifiers under multiple branches, single condition (cancels)', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -238,7 +238,7 @@ defineSuite([ expect(output).not.toContain(notExpected); }); - it ('creates single layout qualifier under multiple branches, multiple conditions (cancels)', function() { + it('creates single layout qualifier under multiple branches, multiple conditions (cancels)', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -257,7 +257,7 @@ defineSuite([ expect(output).not.toContain(notExpected); }); - it ('creates multiple layout qualifiers under multiple branches, multiple conditions (cascades)', function() { + it('creates multiple layout qualifiers under multiple branches, multiple conditions (cascades)', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -279,7 +279,7 @@ defineSuite([ expect(containsExpected0).toBe(true); }); - it ('creates single layout qualifier under multiple branches, single condition (else)', function() { + it('creates single layout qualifier under multiple branches, single condition (else)', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + @@ -296,7 +296,7 @@ defineSuite([ expect(output).not.toContain(notExpected); }); - it ('creates branched layout qualifiers for gl_FragColor and gl_FragData', function() { + it('creates branched layout qualifiers for gl_FragColor and gl_FragData', function() { var noQualifiers = '#define OUTPUT_DECLARATION \n' + '#define EXAMPLE_BRANCH \n' + From 0b210d663e51c950a9110e3c6b1b65f6cb979af5 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:07:45 -0400 Subject: [PATCH 47/57] Changes toThrow to toThrowDeveloperError --- Specs/Renderer/ModernizeShaderSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/Renderer/ModernizeShaderSpec.js b/Specs/Renderer/ModernizeShaderSpec.js index 5e02be5d264b..ac629f78f6e3 100644 --- a/Specs/Renderer/ModernizeShaderSpec.js +++ b/Specs/Renderer/ModernizeShaderSpec.js @@ -35,7 +35,7 @@ defineSuite([ var runFunc = function() { modernizeShader(noOutputDeclaration, true); }; - expect(runFunc).toThrow(); + expect(runFunc).toThrowDeveloperError(); }); it('creates layout qualifier for gl_FragColor', function() { From f0750fecb64355366de4f0dd208c32d7c956dcf2 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:12:44 -0400 Subject: [PATCH 48/57] Renames "ModernizeShader" to "modernizeShader" --- Source/Renderer/ShaderSource.js | 2 +- Source/Renderer/{ModernizeShader.js => modernizeShader.js} | 0 .../Renderer/{ModernizeShaderSpec.js => modernizeShaderSpec.js} | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename Source/Renderer/{ModernizeShader.js => modernizeShader.js} (100%) rename Specs/Renderer/{ModernizeShaderSpec.js => modernizeShaderSpec.js} (99%) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index da96cbdf4526..b1f4684b93df 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -2,7 +2,7 @@ define([ '../Core/defaultValue', '../Core/defined', '../Core/DeveloperError', - '../Renderer/ModernizeShader', + '../Renderer/modernizeShader', '../Shaders/Builtin/CzmBuiltins', './AutomaticUniforms' ], function( diff --git a/Source/Renderer/ModernizeShader.js b/Source/Renderer/modernizeShader.js similarity index 100% rename from Source/Renderer/ModernizeShader.js rename to Source/Renderer/modernizeShader.js diff --git a/Specs/Renderer/ModernizeShaderSpec.js b/Specs/Renderer/modernizeShaderSpec.js similarity index 99% rename from Specs/Renderer/ModernizeShaderSpec.js rename to Specs/Renderer/modernizeShaderSpec.js index ac629f78f6e3..b915fd6461e7 100644 --- a/Specs/Renderer/ModernizeShaderSpec.js +++ b/Specs/Renderer/modernizeShaderSpec.js @@ -1,5 +1,5 @@ defineSuite([ - 'Renderer/ModernizeShader' + 'Renderer/modernizeShader' ], function( modernizeShader) { 'use strict'; From b82a087511d0609df4ffa46d55fbd66e40ca5893 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Mon, 17 Jul 2017 14:16:45 -0400 Subject: [PATCH 49/57] Renames "variablesThatWeCareAbout" to "layoutVariables" --- Source/Renderer/modernizeShader.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index bc078e9d5112..ba649428cb95 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -51,12 +51,12 @@ define([ } } - function getVariablePreprocessorBranch(variablesThatWeCareAbout, splitSource) { + function getVariablePreprocessorBranch(layoutVariables, splitSource) { var variableMap = {}; - var numVariablesWeCareAbout = variablesThatWeCareAbout.length; - for (var a = 0; a < numVariablesWeCareAbout; ++a) { - var variableThatWeCareAbout = variablesThatWeCareAbout[a]; + var numLayoutVariables = layoutVariables.length; + for (var a = 0; a < numLayoutVariables; ++a) { + var variableThatWeCareAbout = layoutVariables[a]; variableMap[variableThatWeCareAbout] = [null]; } @@ -80,8 +80,8 @@ define([ } else if (hasENDIF) { stack.pop(); } else if (!/layout/g.test(line)) { - for (var varIndex = 0; varIndex < numVariablesWeCareAbout; ++varIndex) { - var varName = variablesThatWeCareAbout[varIndex]; + for (var varIndex = 0; varIndex < numLayoutVariables; ++varIndex) { + var varName = layoutVariables[varIndex]; if (line.indexOf(varName) !== -1) { if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { variableMap[varName] = stack.slice(); From 00d8eaba1d7eb7825de335f9aa6678ff1a1d60d1 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 10:29:50 -0400 Subject: [PATCH 50/57] Renames variable "number" and also saves splitSource.length --- Source/Renderer/modernizeShader.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index ba649428cb95..8e2a67623c95 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -12,24 +12,28 @@ define([ var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; var regex = new RegExp(regexStr, 'g'); - for (var number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - splitSource[number] = line.replace(regex, '$1' + replacement + '$3'); + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + var line = splitSource[i]; + splitSource[i] = line.replace(regex, '$1' + replacement + '$3'); } } function replaceInSourceRegex(regex, replacement, splitSource) { - for (var number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - splitSource[number] = line.replace(regex, replacement); + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + var line = splitSource[i]; + splitSource[i] = line.replace(regex, replacement); } } function findInSource(str, splitSource) { var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; var regex = new RegExp(regexStr, 'g'); - for (var number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; + + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + var line = splitSource[i]; if (regex.test(line)) { return true; } @@ -39,8 +43,10 @@ define([ function compileSource(splitSource) { var wholeSource = ''; - for (var number = 0; number < splitSource.length; ++number) { - wholeSource += splitSource[number] + '\n'; + + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + wholeSource += splitSource[i] + '\n'; } return wholeSource; } From c0957a97e1850b9b9cf2d31424c91602ebd9c1a3 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 10:48:49 -0400 Subject: [PATCH 51/57] Renames set1 to set --- Source/Renderer/modernizeShader.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index 8e2a67623c95..764f2fcd876d 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -51,9 +51,9 @@ define([ return wholeSource; } - function setAdd(variable, set1) { - if (set1.indexOf(variable) === -1) { - set1.push(variable); + function setAdd(variable, set) { + if (set.indexOf(variable) === -1) { + set.push(variable); } } From 6b34b077b6ff3e1b1cf5a55a44a98b855f73e94c Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 10:50:06 -0400 Subject: [PATCH 52/57] Moves modernizeShader to the top of the file --- Source/Renderer/modernizeShader.js | 222 ++++++++++++++--------------- 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index 764f2fcd876d..4a5dd91fec22 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -6,117 +6,6 @@ define([ DeveloperError) { 'use strict'; - // Note that this fails if your string looks like - // searchString[singleCharacter]searchString - function replaceInSourceString(str, replacement, splitSource) { - var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; - var regex = new RegExp(regexStr, 'g'); - - var splitSourceLength = splitSource.length; - for (var i = 0; i < splitSourceLength; ++i) { - var line = splitSource[i]; - splitSource[i] = line.replace(regex, '$1' + replacement + '$3'); - } - } - - function replaceInSourceRegex(regex, replacement, splitSource) { - var splitSourceLength = splitSource.length; - for (var i = 0; i < splitSourceLength; ++i) { - var line = splitSource[i]; - splitSource[i] = line.replace(regex, replacement); - } - } - - function findInSource(str, splitSource) { - var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; - var regex = new RegExp(regexStr, 'g'); - - var splitSourceLength = splitSource.length; - for (var i = 0; i < splitSourceLength; ++i) { - var line = splitSource[i]; - if (regex.test(line)) { - return true; - } - } - return false; - } - - function compileSource(splitSource) { - var wholeSource = ''; - - var splitSourceLength = splitSource.length; - for (var i = 0; i < splitSourceLength; ++i) { - wholeSource += splitSource[i] + '\n'; - } - return wholeSource; - } - - function setAdd(variable, set) { - if (set.indexOf(variable) === -1) { - set.push(variable); - } - } - - function getVariablePreprocessorBranch(layoutVariables, splitSource) { - var variableMap = {}; - - var numLayoutVariables = layoutVariables.length; - for (var a = 0; a < numLayoutVariables; ++a) { - var variableThatWeCareAbout = layoutVariables[a]; - variableMap[variableThatWeCareAbout] = [null]; - } - - var stack = []; - for (var i = 0; i < splitSource.length; ++i) { - var line = splitSource[i]; - var hasIF = /(#ifdef|#if)/g.test(line); - var hasELSE = /#else/g.test(line); - var hasENDIF = /#endif/g.test(line); - - if (hasIF) { - stack.push(line); - } else if (hasELSE) { - var top = stack[stack.length - 1]; - var op = top.replace('ifdef', 'ifndef'); - if (/if/g.test(op)) { - op = op.replace(/(#if\s+)(\S*)([^]*)/, '$1!($2)$3'); - } - stack.pop(); - stack.push(op); - } else if (hasENDIF) { - stack.pop(); - } else if (!/layout/g.test(line)) { - for (var varIndex = 0; varIndex < numLayoutVariables; ++varIndex) { - var varName = layoutVariables[varIndex]; - if (line.indexOf(varName) !== -1) { - if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { - variableMap[varName] = stack.slice(); - } else { - variableMap[varName] = variableMap[varName].filter(function(x) { - return stack.indexOf(x) >= 0; - }); - } - } - } - } - } - - for (var care in variableMap) { - if (variableMap.hasOwnProperty(care)) { - if (variableMap.length === 1 && variableMap[0] === null) { - variableMap.splice(0, 1); - } - } - } - - return variableMap; - } - - function removeExtension(name, splitSource) { - var regex = '#extension\\s+GL_' + name + '\\s+:\\s+[a-zA-Z0-9]+\\s*$'; - replaceInSourceRegex(new RegExp(regex, 'g'), '', splitSource); - } - /** * A function to port GLSL shaders from GLSL ES 1.00 to GLSL ES 3.00 * @@ -235,5 +124,116 @@ define([ return compileSource(splitSource); } + // Note that this fails if your string looks like + // searchString[singleCharacter]searchString + function replaceInSourceString(str, replacement, splitSource) { + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; + var regex = new RegExp(regexStr, 'g'); + + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + var line = splitSource[i]; + splitSource[i] = line.replace(regex, '$1' + replacement + '$3'); + } + } + + function replaceInSourceRegex(regex, replacement, splitSource) { + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + var line = splitSource[i]; + splitSource[i] = line.replace(regex, replacement); + } + } + + function findInSource(str, splitSource) { + var regexStr = '(^|[^\\w])(' + str + ')($|[^\\w])'; + var regex = new RegExp(regexStr, 'g'); + + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + var line = splitSource[i]; + if (regex.test(line)) { + return true; + } + } + return false; + } + + function compileSource(splitSource) { + var wholeSource = ''; + + var splitSourceLength = splitSource.length; + for (var i = 0; i < splitSourceLength; ++i) { + wholeSource += splitSource[i] + '\n'; + } + return wholeSource; + } + + function setAdd(variable, set) { + if (set.indexOf(variable) === -1) { + set.push(variable); + } + } + + function getVariablePreprocessorBranch(layoutVariables, splitSource) { + var variableMap = {}; + + var numLayoutVariables = layoutVariables.length; + for (var a = 0; a < numLayoutVariables; ++a) { + var variableThatWeCareAbout = layoutVariables[a]; + variableMap[variableThatWeCareAbout] = [null]; + } + + var stack = []; + for (var i = 0; i < splitSource.length; ++i) { + var line = splitSource[i]; + var hasIF = /(#ifdef|#if)/g.test(line); + var hasELSE = /#else/g.test(line); + var hasENDIF = /#endif/g.test(line); + + if (hasIF) { + stack.push(line); + } else if (hasELSE) { + var top = stack[stack.length - 1]; + var op = top.replace('ifdef', 'ifndef'); + if (/if/g.test(op)) { + op = op.replace(/(#if\s+)(\S*)([^]*)/, '$1!($2)$3'); + } + stack.pop(); + stack.push(op); + } else if (hasENDIF) { + stack.pop(); + } else if (!/layout/g.test(line)) { + for (var varIndex = 0; varIndex < numLayoutVariables; ++varIndex) { + var varName = layoutVariables[varIndex]; + if (line.indexOf(varName) !== -1) { + if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { + variableMap[varName] = stack.slice(); + } else { + variableMap[varName] = variableMap[varName].filter(function(x) { + return stack.indexOf(x) >= 0; + }); + } + } + } + } + } + + for (var care in variableMap) { + if (variableMap.hasOwnProperty(care)) { + if (variableMap.length === 1 && variableMap[0] === null) { + variableMap.splice(0, 1); + } + } + } + + return variableMap; + } + + function removeExtension(name, splitSource) { + var regex = '#extension\\s+GL_' + name + '\\s+:\\s+[a-zA-Z0-9]+\\s*$'; + replaceInSourceRegex(new RegExp(regex, 'g'), '', splitSource); + } + return modernizeShader; }); From 7251f2ae94e96b502ab75c77740997b7b7bf8090 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 10:59:20 -0400 Subject: [PATCH 53/57] All the counters in modernizeShader now use "i" --- Source/Renderer/modernizeShader.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index 4a5dd91fec22..f7ed7034b2ad 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -27,11 +27,11 @@ define([ } var outputDeclarationLine = -1; - var number; - for (number = 0; number < splitSource.length; ++number) { - var line = splitSource[number]; - if (outputDeclarationRegex.exec(line)) { - outputDeclarationLine = number; + var i; + for (i = 0; i < splitSource.length; ++i) { + var line = splitSource[i]; + if (outputDeclarationRegex.test(line)) { + outputDeclarationLine = i; break; } } @@ -42,7 +42,7 @@ define([ var outputVariables = []; - for (var i = 0; i < 10; i++) { + for (i = 0; i < 10; i++) { var fragDataString = 'gl_FragData\\[' + i + '\\]'; var newOutput = 'czm_out' + i; var regex = new RegExp(fragDataString, 'g'); @@ -64,8 +64,8 @@ define([ var variableMap = getVariablePreprocessorBranch(outputVariables, splitSource); var lineAdds = {}; - for (var c = 0; c < splitSource.length; c++) { - var l = splitSource[c]; + for (i = 0; i < splitSource.length; i++) { + var l = splitSource[i]; for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { var matchVar = new RegExp('(layout)[^]+(out)[^]+(' + care + ')[^]+', 'g'); @@ -95,9 +95,9 @@ define([ var versionThree = '#version 300 es'; var foundVersion = false; - for (number = 0; number < splitSource.length; number++) { - if (/#version/.test(splitSource[number])) { - splitSource[number] = versionThree; + for (i = 0; i < splitSource.length; i++) { + if (/#version/.test(splitSource[i])) { + splitSource[i] = versionThree; foundVersion = true; } } From 968a28d41e810c51103b2e011d05ae255f4a71eb Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 11:02:40 -0400 Subject: [PATCH 54/57] Renames "l" to "line" --- Source/Renderer/modernizeShader.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index f7ed7034b2ad..8aa7a1bab97f 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -27,9 +27,9 @@ define([ } var outputDeclarationLine = -1; - var i; + var i, line; for (i = 0; i < splitSource.length; ++i) { - var line = splitSource[i]; + line = splitSource[i]; if (outputDeclarationRegex.test(line)) { outputDeclarationLine = i; break; @@ -65,12 +65,12 @@ define([ var variableMap = getVariablePreprocessorBranch(outputVariables, splitSource); var lineAdds = {}; for (i = 0; i < splitSource.length; i++) { - var l = splitSource[i]; + line = splitSource[i]; for (var care in variableMap) { if (variableMap.hasOwnProperty(care)) { var matchVar = new RegExp('(layout)[^]+(out)[^]+(' + care + ')[^]+', 'g'); - if (matchVar.test(l)) { - lineAdds[l] = care; + if (matchVar.test(line)) { + lineAdds[line] = care; } } } From ff24d72b19c2f0300a702ba4e8306afd202340ec Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 11:06:29 -0400 Subject: [PATCH 55/57] Renames "care" --- Source/Renderer/modernizeShader.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index 8aa7a1bab97f..b07d3b06a3e1 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -66,11 +66,11 @@ define([ var lineAdds = {}; for (i = 0; i < splitSource.length; i++) { line = splitSource[i]; - for (var care in variableMap) { - if (variableMap.hasOwnProperty(care)) { - var matchVar = new RegExp('(layout)[^]+(out)[^]+(' + care + ')[^]+', 'g'); + for (var variable in variableMap) { + if (variableMap.hasOwnProperty(variable)) { + var matchVar = new RegExp('(layout)[^]+(out)[^]+(' + variable + ')[^]+', 'g'); if (matchVar.test(line)) { - lineAdds[line] = care; + lineAdds[line] = variable; } } } @@ -219,8 +219,8 @@ define([ } } - for (var care in variableMap) { - if (variableMap.hasOwnProperty(care)) { + for (var variable in variableMap) { + if (variableMap.hasOwnProperty(variable)) { if (variableMap.length === 1 && variableMap[0] === null) { variableMap.splice(0, 1); } From 7a7da05e185d5391bc67b58b48de35b565d59944 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Tue, 18 Jul 2017 11:21:32 -0400 Subject: [PATCH 56/57] Cleanup of unused variables --- Source/Renderer/modernizeShader.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/Source/Renderer/modernizeShader.js b/Source/Renderer/modernizeShader.js index b07d3b06a3e1..b2accdf7e62a 100644 --- a/Source/Renderer/modernizeShader.js +++ b/Source/Renderer/modernizeShader.js @@ -179,10 +179,6 @@ define([ var variableMap = {}; var numLayoutVariables = layoutVariables.length; - for (var a = 0; a < numLayoutVariables; ++a) { - var variableThatWeCareAbout = layoutVariables[a]; - variableMap[variableThatWeCareAbout] = [null]; - } var stack = []; for (var i = 0; i < splitSource.length; ++i) { @@ -207,7 +203,7 @@ define([ for (var varIndex = 0; varIndex < numLayoutVariables; ++varIndex) { var varName = layoutVariables[varIndex]; if (line.indexOf(varName) !== -1) { - if (variableMap[varName].length === 1 && variableMap[varName][0] === null) { + if (!defined(variableMap[varName])) { variableMap[varName] = stack.slice(); } else { variableMap[varName] = variableMap[varName].filter(function(x) { @@ -219,14 +215,6 @@ define([ } } - for (var variable in variableMap) { - if (variableMap.hasOwnProperty(variable)) { - if (variableMap.length === 1 && variableMap[0] === null) { - variableMap.splice(0, 1); - } - } - } - return variableMap; } From a05a976d1482121eefc2cc5722fb9aac097aa203 Mon Sep 17 00:00:00 2001 From: Srinivas Kaza Date: Tue, 18 Jul 2017 14:29:07 -0400 Subject: [PATCH 57/57] Adds support for the EXT_blend_minmax extension --- Source/Renderer/Context.js | 15 +++++++++++++++ Source/Renderer/RenderState.js | 4 +++- Source/Scene/BlendEquation.js | 22 ++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Source/Renderer/Context.js b/Source/Renderer/Context.js index b92bfa2e916c..5ab984919331 100644 --- a/Source/Renderer/Context.js +++ b/Source/Renderer/Context.js @@ -269,6 +269,7 @@ define([ // Query and initialize extensions this._standardDerivatives = !!getExtension(gl, ['OES_standard_derivatives']); + this._blendMinmax = !!getExtension(gl, ['EXT_blend_minmax']); this._elementIndexUint = !!getExtension(gl, ['OES_element_index_uint']); this._depthTexture = !!getExtension(gl, ['WEBGL_depth_texture', 'WEBKIT_WEBGL_depth_texture']); this._textureFloat = !!getExtension(gl, ['OES_texture_float']); @@ -489,6 +490,20 @@ define([ } }, + /** + * true if the EXT_blend_minmax extension is supported. This + * extension extends blending capabilities by adding two new blend equations: + * the minimum or maximum color components of the source and destination colors. + * @memberof Context.prototype + * @type {Boolean} + * @see {@link https://www.khronos.org/registry/webgl/extensions/EXT_blend_minmax/} + */ + blendMinmax : { + get : function() { + return this._blendMinmax || this._webgl2; + } + }, + /** * true if the OES_element_index_uint extension is supported. This * extension allows the use of unsigned int indices, which can improve performance by diff --git a/Source/Renderer/RenderState.js b/Source/Renderer/RenderState.js index 3b30bebbd290..07ab7633fe12 100644 --- a/Source/Renderer/RenderState.js +++ b/Source/Renderer/RenderState.js @@ -21,7 +21,9 @@ define([ function validateBlendEquation(blendEquation) { return ((blendEquation === WebGLConstants.FUNC_ADD) || (blendEquation === WebGLConstants.FUNC_SUBTRACT) || - (blendEquation === WebGLConstants.FUNC_REVERSE_SUBTRACT)); + (blendEquation === WebGLConstants.FUNC_REVERSE_SUBTRACT) || + (blendEquation === WebGLConstants.MIN) || + (blendEquation === WebGLConstants.MAX)); } function validateBlendFunction(blendFunction) { diff --git a/Source/Scene/BlendEquation.js b/Source/Scene/BlendEquation.js index 27bf019d21eb..e589beeae418 100644 --- a/Source/Scene/BlendEquation.js +++ b/Source/Scene/BlendEquation.js @@ -34,9 +34,27 @@ define([ * @type {Number} * @constant */ - REVERSE_SUBTRACT : WebGLConstants.FUNC_REVERSE_SUBTRACT + REVERSE_SUBTRACT : WebGLConstants.FUNC_REVERSE_SUBTRACT, - // No min and max like in ColladaFX GLES2 profile + /** + * Pixel values are given to the minimum function (min(source, destination)). + * + * This equation operates on each pixel color component. + * + * @type {Number} + * @constant + */ + MIN : WebGLConstants.MIN, + + /** + * Pixel values are given to the maximum function (max(source, destination)). + * + * This equation operates on each pixel color component. + * + * @type {Number} + * @constant + */ + MAX : WebGLConstants.MAX }; return freezeObject(BlendEquation);