Skip to content

Commit

Permalink
WGSL transpiler update to namespace the results
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Nov 1, 2023
1 parent e00dac2 commit 2461997
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion js/common/Transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Transpiler {

// NOTE: Will be able to use wgslMangle in the future?
// NOTE: We could also potentially feed this through the transform (source-maps wouldn't really be useful)
js = wgslPreprocess( wgslStripComments( text ), this.minifyWGSL ? wgslMinify : str => str, pathToRoot );
js = wgslPreprocess( wgslStripComments( text ), this.minifyWGSL ? wgslMinify : str => str, pathToRoot, targetPath );
}
else {
js = core.transformSync( text, {
Expand Down
26 changes: 17 additions & 9 deletions js/common/wgslPreprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

const _ = require( 'lodash' );
const path = require( 'path' );

const importString = '#import ';
const bindingsString = '#bindings';
Expand Down Expand Up @@ -70,20 +71,21 @@ class Code {
}

// @public
toString( indent, pathToRoot ) {
toString( indent, pathToRoot, moduleName, repoName ) {
let result = '';

if ( this.isRoot ) {
result += `// Copyright ${new Date().getFullYear()}, University of Colorado Boulder\n\n`;

result += `import { u32, i32, f32 } from '${pathToRoot}alpenglow/js/imports.js'\n`;
result += `import { ${repoName} } from '${pathToRoot}${repoName}/js/imports.js'\n`;
const imports = _.uniq( this.allImports ).sort();
imports.forEach( importString => {
result += `import ${importStringToImportName( importString )} from '${importString}.js';\n`;
} );

result += '\n';
result += `export default ${this.hasConditionalsOrTemplates() ? 'options' : '()'} => `;
result += `const ${moduleName} = ${this.hasConditionalsOrTemplates() ? 'options' : '()'} => `;
}

const run = ( item, before ) => {
Expand All @@ -92,7 +94,7 @@ class Code {
}
else {
// a Conditional
result += item.toString( indent, pathToRoot );
result += item.toString( indent, pathToRoot, moduleName, repoName );
}
};

Expand Down Expand Up @@ -133,6 +135,9 @@ class Code {
result += '}';
}
result += ';\n';

result += `export default ${moduleName};\n`;
result += `${repoName}.register( '${moduleName}', ${moduleName} );\n`;
}
else {
if ( this.imports.length ) {
Expand All @@ -158,7 +163,7 @@ class Conditional {
}

// @public
toString( indent, pathToRoot ) {
toString( indent, pathToRoot, moduleName, repoName ) {
let result = '';

if ( this.included.isEmpty() && this.excluded.isEmpty() ) {
Expand All @@ -167,16 +172,16 @@ class Conditional {

if ( this.included.isEmpty() ) {
result += `${indent}if ( !options[ ${JSON.stringify( this.name )} ] ) {\n`;
result += this.excluded.toString( indent + ' ', pathToRoot );
result += this.excluded.toString( indent + ' ', pathToRoot, moduleName, repoName );
result += `${indent}}\n`;
}
else {
result += `${indent}if ( options[ ${JSON.stringify( this.name )} ] ) {\n`;
result += this.included.toString( indent + ' ', pathToRoot );
result += this.included.toString( indent + ' ', pathToRoot, moduleName, repoName );
result += `${indent}}\n`;
if ( !this.excluded.isEmpty() ) {
result += `${indent}else {\n`;
result += this.excluded.toString( indent + ' ', pathToRoot );
result += this.excluded.toString( indent + ' ', pathToRoot, moduleName, repoName );
result += `${indent}}\n`;
}
}
Expand All @@ -185,11 +190,14 @@ class Conditional {
}
}

const wgslPreprocess = ( str, minify, pathToRoot ) => {
const wgslPreprocess = ( str, minify, pathToRoot, targetPath ) => {

// sanity check
str = str.replace( /\r\n/g, '\n' );

const repoName = path.basename( path.resolve( targetPath, pathToRoot ) );
const moduleName = `wgsl_${path.basename( targetPath ).split( '.' )[ 0 ]}`;

const lines = str.split( '\n' );

const rootCode = new Code( true );
Expand Down Expand Up @@ -258,7 +266,7 @@ const wgslPreprocess = ( str, minify, pathToRoot ) => {
throw new Error( 'unterminated conditional' );
}

return rootCode.toString( ' ', pathToRoot );
return rootCode.toString( ' ', pathToRoot, moduleName, repoName );
};

module.exports = wgslPreprocess;

0 comments on commit 2461997

Please sign in to comment.