Skip to content

Commit

Permalink
Fix GLSL parsing to ignore preprocessor statements. Bump version and …
Browse files Browse the repository at this point in the history
…build files.
  • Loading branch information
kbirk committed Jul 18, 2016
1 parent e505a3f commit 66d6954
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 41 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esper",
"version": "0.10.3",
"version": "0.10.4",
"description": "A low-level WebGL rendering framework",
"homepage": "https://github.com/kbirk/esper",
"authors": [
Expand Down
24 changes: 20 additions & 4 deletions build/esper.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/esper.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/ColorTexture2D.html
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/ColorTexture2D.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ <h1 class="page-title">ColorTexture2D.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/DepthTexture2D.html
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/DepthTexture2D.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ <h1 class="page-title">DepthTexture2D.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/IndexBuffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/IndexBuffer.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ <h1 class="page-title">IndexBuffer.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/RenderTarget.html
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/RenderTarget.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ <h1 class="page-title">RenderTarget.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Renderable.html
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Renderable.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ <h1 class="page-title">Renderable.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Shader.html
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Shader.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ <h1 class="page-title">Shader.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
24 changes: 20 additions & 4 deletions docs/ShaderParser.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ <h1 class="page-title">ShaderParser.js</h1>
var NAME_COUNT_REGEXP = /([a-zA-Z_][a-zA-Z0-9_]*)(?:\[(\d+)\])?/;
var PRECISION_REGEX = /\b(precision)\s+(\w+)\s+(\w+)/;
var GLSL_REGEXP = /void\s+main\s*\(\s*(void)*\s*\)\s*/mi;
var PREP_REGEXP = /#(define|if|ifdef|ifndef|else|elif|endif|undef|error|pragma|extension|version|line).*\n/g;

/**
* Removes standard comments from the provided string.
Expand Down Expand Up @@ -202,7 +203,7 @@ <h1 class="page-title">ShaderParser.js</h1>
var statements = normalized.split(';');
// build regex for parsing statements with targetted keywords
var keywordStr = keywords.join('|');
var keywordRegex = new RegExp( '.*\\b(' + keywordStr + ')\\b.*' );
var keywordRegex = new RegExp( '\\b(' + keywordStr + ')\\b.*' );
// parse and store global precision statements and any declarations
var precisions = {};
var matched = [];
Expand Down Expand Up @@ -231,7 +232,8 @@ <h1 class="page-title">ShaderParser.js</h1>
}

/**
* Filters out duplicate declarations present between shaders.
* Filters out duplicate declarations present between shaders. Currently
* just removes all # statements.
* @private
*
* @param {Array} declarations - The array of declarations.
Expand All @@ -251,6 +253,19 @@ <h1 class="page-title">ShaderParser.js</h1>
});
}

/**
* Runs the preprocessor on the glsl code.
* @private
*
* @param {String} source - The unprocessed source code.
*
* @returns {String} The processed source code.
*/
function preprocess( source ) {
// TODO: implement this correctly...
return source.replace(PREP_REGEXP, '');
}

module.exports = {

/**
Expand Down Expand Up @@ -283,7 +298,8 @@ <h1 class="page-title">ShaderParser.js</h1>
// parse out targetted declarations
var declarations = [];
sources.forEach( function( source ) {
declarations = declarations.concat( parseSource( source, qualifiers ) );
var preprocessed = preprocess( source );
declarations = declarations.concat( parseSource( preprocessed, qualifiers ) );
});
// remove duplicates and return
return filterDuplicatesByName( declarations );
Expand Down Expand Up @@ -315,7 +331,7 @@ <h1 class="page-title">ShaderParser.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Texture2D.html
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Texture2D.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ <h1 class="page-title">Texture2D.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/TextureCubeMap.html
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/TextureCubeMap.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ <h1 class="page-title">TextureCubeMap.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/VertexBuffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/VertexBuffer.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ <h1 class="page-title">VertexBuffer.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/VertexPackage.html
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/VertexPackage.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ <h1 class="page-title">VertexPackage.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Viewport.html
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ <h5>Returns:</h5>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Viewport.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ <h1 class="page-title">Viewport.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/WebGLContext.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ <h1 class="page-title">WebGLContext.js</h1>
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ <h2>Example</h2><ul>
mode: TRIANGLES
});

// Draw points from a byte offset.
// Draw points from an index offset.
vertexBuffer.draw({
mode: POINTS,
byteOffset: 100 * 4 * 2
indexOffset: 100
});

// Draw n lines.
Expand Down Expand Up @@ -506,7 +506,7 @@ <h3>DepthTextures</h3><p>Depth textures can be used to store depth values and ar
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jun 09 2016 17:00:56 GMT-0400 (EDT) using the Minami theme.
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jul 18 2016 17:00:27 GMT-0400 (EDT) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esper",
"version": "0.10.3",
"version": "0.10.4",
"description": "A low-level WebGL rendering framework",
"author": "Kevin Birk <birk.kevin@gmail.com>",
"main": "src/exports.js",
Expand Down
22 changes: 19 additions & 3 deletions src/core/ShaderParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
var NAME_COUNT_REGEXP = /([a-zA-Z_][a-zA-Z0-9_]*)(?:\[(\d+)\])?/;
var PRECISION_REGEX = /\b(precision)\s+(\w+)\s+(\w+)/;
var GLSL_REGEXP = /void\s+main\s*\(\s*(void)*\s*\)\s*/mi;
var PREP_REGEXP = /#(define|if|ifdef|ifndef|else|elif|endif|undef|error|pragma|extension|version|line).*\n/g;

/**
* Removes standard comments from the provided string.
Expand Down Expand Up @@ -162,7 +163,7 @@
var statements = normalized.split(';');
// build regex for parsing statements with targetted keywords
var keywordStr = keywords.join('|');
var keywordRegex = new RegExp( '.*\\b(' + keywordStr + ')\\b.*' );
var keywordRegex = new RegExp( '\\b(' + keywordStr + ')\\b.*' );
// parse and store global precision statements and any declarations
var precisions = {};
var matched = [];
Expand Down Expand Up @@ -191,7 +192,8 @@
}

/**
* Filters out duplicate declarations present between shaders.
* Filters out duplicate declarations present between shaders. Currently
* just removes all # statements.
* @private
*
* @param {Array} declarations - The array of declarations.
Expand All @@ -211,6 +213,19 @@
});
}

/**
* Runs the preprocessor on the glsl code.
* @private
*
* @param {String} source - The unprocessed source code.
*
* @returns {String} The processed source code.
*/
function preprocess( source ) {
// TODO: implement this correctly...
return source.replace(PREP_REGEXP, '');
}

module.exports = {

/**
Expand Down Expand Up @@ -243,7 +258,8 @@
// parse out targetted declarations
var declarations = [];
sources.forEach( function( source ) {
declarations = declarations.concat( parseSource( source, qualifiers ) );
var preprocessed = preprocess( source );
declarations = declarations.concat( parseSource( preprocessed, qualifiers ) );
});
// remove duplicates and return
return filterDuplicatesByName( declarations );
Expand Down
12 changes: 12 additions & 0 deletions test/core/ShaderParserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
var source = [
'uniform highp mat4 A[ 10], B, C [2];',
'uniform highp mat4 D;',
'float func() {',
'return 5.0;',
'}',
'uniform highp mat4 E ,',
'F ',
'[11 ] ;',
Expand All @@ -169,6 +172,15 @@
assert( declarations[0].precision === 'highp' );
assert( declarations[1].precision === 'mediump' );
});
it('should handle preprocessor statements', function() {
var source = [
'precision highp float;',
'#define TEST 5',
'#define CONST 6',
'void main() { ... }'].join('\n');
var declarations = ShaderParser.parseDeclarations( source, [ 'uniform', 'attribute' ] );
assert( declarations.length === 0 );
});
});
describe('#isGLSL()', function() {
it('should return true if the argument string contains a `void main(...) {}`', function() {
Expand Down

0 comments on commit 66d6954

Please sign in to comment.