Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added #include_once shader directive and circular inclusion detection #13282

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,53 @@ function replaceClippingPlaneNums( string, parameters ) {

}

function parseIncludes( string ) {
function parseIncludes( string, stack, stackMap, alreadyIncluded ) {

var pattern = /^[ \t]*#include +<([\w\d./]+)>/gm;
var pattern = /^[ \t]*#include(_once|) +<([\w\d./]+)>/gm;

function replace( match, include ) {
if ( stack === undefined ) {

stack = [];
alreadyIncluded = {};
stackMap = {};

}

function replace( match, once, include ) {

if ( once.length !== 0 && alreadyIncluded[ include ] === true ) {

return '';

}

if ( stackMap[ include ] === true ) {

throw new Error(
'Circular #include' + once + ' <' + include + '> detected!\n(\n\t' + stack.join( '\n\t' ) + '\n)'
);

}

var replace = ShaderChunk[ include ];

if ( replace === undefined ) {

throw new Error( 'Can not resolve #include <' + include + '>' );
throw new Error(
'Can not resolve #include' + once + ' <' + include + '>'
+ ( stack.length !== 0 ? '\n(\n\t' + stack.join( '\n\t' ) + '\n)' : '' )
);

}

return parseIncludes( replace );
stack.push( include );
stackMap[ include ] = true;
alreadyIncluded[ include ] = true;
var result = parseIncludes( replace, stack, stackMap, alreadyIncluded );
stack.pop();
stackMap[ include ] = false;

return result;

}

Expand Down Expand Up @@ -727,4 +759,4 @@ function WebGLProgram( renderer, extensions, code, material, shader, parameters,

}

export { WebGLProgram };
export { WebGLProgram, parseIncludes };
81 changes: 80 additions & 1 deletion test/unit/src/renderers/webgl/WebGLProgram.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@
*/
/* global QUnit */

import { WebGLProgram } from '../../../../../src/renderers/webgl/WebGLProgram';
import { parseIncludes } from '../../../../../src/renderers/webgl/WebGLProgram';
import { ShaderChunk } from '../../../../../src/renderers/shaders/ShaderChunk';

function copyShaderChunks( chunks ) {

var copy = {};
for ( var k in chunks ) {

copy[ k ] = chunks[ k ];

}

return copy;

}

function merge( target, source ) {

for ( var k in source ) {

target[ k ] = source[ k ];

}

}

export default QUnit.module( 'Renderers', () => {

Expand Down Expand Up @@ -50,6 +74,61 @@ export default QUnit.module( 'Renderers', () => {

} );

QUnit.test( "parseIncludes", ( assert ) => {

var testChunks = {
test_chunk_a: [
"#chunk_a#"
].join( "\n" ),
test_chunk_b: [
"#include_once <test_chunk_a>",
"#include_once <test_chunk_f>",
"#include_once <test_chunk_a>"
].join( "\n" ),
test_chunk_c: [
"#include <test_chunk_a>",
"#include <test_chunk_d>"
].join( "\n" ),
test_chunk_d: [
"#include <test_chunk_e>",
"#include <test_chunk_a>"
].join( "\n" ),
test_chunk_e: [
"#include <test_chunk_c>",
"#include <test_chunk_a>"
].join( "\n" ),
test_chunk_f: [
"#chunk_f#"
].join( "\n" )
};
merge( ShaderChunk, testChunks );
var testStack = [ "test_chunk_c", "test_chunk_d", "test_chunk_e" ];
assert.throws(
() => {

parseIncludes( "#include_once <test_chunk_c>" );

},
new Error(
'Circular #include <test_chunk_c> detected!\n(\n\t' + testStack.join( '\n\t' ) + '\n)'
),
"throws error on circular dependencies"
);

assert.equal(
parseIncludes(
"#include <test_chunk_b>"
),
[
"#chunk_a#",
"#chunk_f#",
""
].join( "\n" ),
"multiple include_once of the same library get only included once"
);

} );

} );

} );
Expand Down