Skip to content

Commit b1d24bd

Browse files
authored
TSL Transpiler: Add simplified Fn() layout (#31299)
* fix arithmetic operators check * add simplified layout
1 parent 8f21de2 commit b1d24bd

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

examples/jsm/transpiler/GLSLDecoder.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const unaryOperators = [
44
'+', '-', '~', '!', '++', '--'
55
];
66

7+
const arithmeticOperators = [
8+
'*', '/', '%', '+', '-', '<<', '>>'
9+
];
10+
711
const precedenceOperators = [
812
'*', '/', '%',
913
'-', '+',
@@ -328,7 +332,11 @@ class GLSLDecoder {
328332
if ( ! token.isOperator || i === 0 || i === tokens.length - 1 ) return;
329333

330334
// important for negate operator after arithmetic operator: a * -1, a * -( b )
331-
if ( ( inverse && tokens[ i - 1 ].isOperator ) || ( ! inverse && tokens[ i + 1 ].isOperator ) ) return;
335+
if ( ( inverse && arithmeticOperators.includes( tokens[ i - 1 ].str ) ) || ( ! inverse && arithmeticOperators.includes( tokens[ i + 1 ].str ) ) ) {
336+
337+
return;
338+
339+
}
332340

333341
if ( groupIndex === 0 && token.str === operator ) {
334342

examples/jsm/transpiler/TSLEncoder.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class TSLEncoder {
5858
this.global = new Set();
5959
this.overloadings = new Map();
6060
this.iife = false;
61-
this.uniqueNames = false;
6261
this.reference = false;
6362

6463
this._currentVariable = null;
@@ -726,8 +725,6 @@ ${ this.tab }} )`;
726725

727726
for ( const param of node.params ) {
728727

729-
let str = `{ name: '${ param.name }', type: '${ param.type }'`;
730-
731728
let name = param.name;
732729

733730
if ( param.immutable === false && ( param.qualifier !== 'inout' && param.qualifier !== 'out' ) ) {
@@ -746,11 +743,9 @@ ${ this.tab }} )`;
746743

747744
}
748745

749-
str += ', qualifier: \'' + param.qualifier + '\'';
750-
751746
}
752747

753-
inputs.push( str + ' }' );
748+
inputs.push( param.name + ': \'' + param.type + '\'' );
754749
params.push( name );
755750

756751
this._currentProperties[ name ] = param;
@@ -795,23 +790,15 @@ ${ this.tab }} )`;
795790
796791
${ bodyStr }
797792
798-
${ this.tab }} )`;
799-
800-
const layoutInput = inputs.length > 0 ? '\n\t\t' + this.tab + inputs.join( ',\n\t\t' + this.tab ) + '\n\t' + this.tab : '';
793+
${ this.tab }}`;
801794

802795
if ( node.layout !== false && hasPointer === false ) {
803796

804-
const uniqueName = this.uniqueNames ? fnName + '_' + Math.random().toString( 36 ).slice( 2 ) : fnName;
805-
806-
funcStr += `.setLayout( {
807-
${ this.tab }\tname: '${ uniqueName }',
808-
${ this.tab }\ttype: '${ type }',
809-
${ this.tab }\tinputs: [${ layoutInput }]
810-
${ this.tab }} )`;
797+
funcStr += ', { ' + inputs.join( ', ' ) + ', return: \'' + type + '\' }';
811798

812799
}
813800

814-
funcStr += ';\n';
801+
funcStr += ' );\n';
815802

816803
this.imports.add( 'Fn' );
817804

0 commit comments

Comments
 (0)