Skip to content

Commit bd9ac7d

Browse files
Mugen87sunag
andauthored
TSL: Add increment and decrement and fix unsual for() expression syntax for transpiler (#30912)
* TSLEncoder: Remove `While` usage. * Revert "TSLEncoder: Remove `While` usage." This reverts commit 28957ed. * fix afterthought for operators * fix While -> Loop * fix `++` operator after code block * optimize increment and decrement * add increment and decrement * Update OperatorNode.js --------- Co-authored-by: sunag <[email protected]>
1 parent 6ff21e0 commit bd9ac7d

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

examples/jsm/transpiler/GLSLDecoder.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ class GLSLDecoder {
414414

415415
}
416416

417-
418417
// unary operators (after)
419418

420419
if ( lastToken.isOperator ) {
@@ -894,7 +893,7 @@ class GLSLDecoder {
894893

895894
//
896895

897-
if ( token.isLiteral ) {
896+
if ( token.isLiteral || token.isOperator ) {
898897

899898
if ( token.str === 'const' ) {
900899

examples/jsm/transpiler/TSLEncoder.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class TSLEncoder {
6161
this.uniqueNames = false;
6262
this.reference = false;
6363

64+
this._currentVarible = null;
65+
6466
this._currentProperties = {};
6567
this._lastStatement = null;
6668

@@ -321,9 +323,22 @@ class TSLEncoder {
321323

322324
let type = unaryLib[ node.type ];
323325

324-
if ( node.after === false && ( node.type === '++' || node.type === '--' ) ) {
326+
if ( node.type === '++' || node.type === '--' ) {
327+
328+
if ( this._currentVarible === null ) {
329+
330+
// optimize increment/decrement operator
331+
// to avoid creating a new variable
332+
333+
node.after = false;
334+
335+
}
336+
337+
if ( node.after === false ) {
338+
339+
type += 'Before';
325340

326-
type += 'Before';
341+
}
327342

328343
}
329344

@@ -476,8 +491,10 @@ ${ this.tab }} )`;
476491

477492
if ( ( initialization && initialization.isVariableDeclaration && initialization.next === null ) &&
478493
( condition && condition.left.isAccessor && condition.left.property === initialization.name ) &&
479-
( afterthought && afterthought.isUnary ) &&
480-
( initialization.name === afterthought.expression.property )
494+
( afterthought && (
495+
( afterthought.isUnary && ( initialization.name === afterthought.expression.property ) ) ||
496+
( afterthought.isOperator && ( initialization.name === afterthought.left.property ) )
497+
) )
481498
) {
482499

483500
return this.emitLoop( node );
@@ -497,7 +514,7 @@ ${ this.tab }} )`;
497514
this.tab += '\t';
498515

499516
let forStr = '{\n\n' + this.tab + initialization + ';\n\n';
500-
forStr += `${ this.tab }While( ${ condition }, () => {\n\n`;
517+
forStr += `${ this.tab }Loop( ${ condition }, () => {\n\n`;
501518

502519
forStr += this.emitBody( node.body ) + '\n\n';
503520

@@ -509,7 +526,7 @@ ${ this.tab }} )`;
509526

510527
forStr += this.tab + '}';
511528

512-
this.imports.add( 'While' );
529+
this.imports.add( 'Loop' );
513530

514531
return forStr;
515532

@@ -519,6 +536,8 @@ ${ this.tab }} )`;
519536

520537
const { name, type, value, next } = node;
521538

539+
this._currentVarible = node;
540+
522541
const valueStr = value ? this.emitExpression( value ) : '';
523542

524543
let varStr = isRoot ? 'const ' : '';
@@ -556,6 +575,8 @@ ${ this.tab }} )`;
556575

557576
this.addImport( type );
558577

578+
this._currentVarible = null;
579+
559580
return varStr;
560581

561582
}

src/Three.TSL.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ export const dFdx = TSL.dFdx;
130130
export const dFdy = TSL.dFdy;
131131
export const dashSize = TSL.dashSize;
132132
export const debug = TSL.debug;
133+
export const decrement = TSL.decrement;
134+
export const decrementBefore = TSL.decrementBefore;
133135
export const defaultBuildStages = TSL.defaultBuildStages;
134136
export const defaultShaderStages = TSL.defaultShaderStages;
135137
export const defined = TSL.defined;
@@ -194,6 +196,8 @@ export const hash = TSL.hash;
194196
export const highpModelNormalViewMatrix = TSL.highpModelNormalViewMatrix;
195197
export const highpModelViewMatrix = TSL.highpModelViewMatrix;
196198
export const hue = TSL.hue;
199+
export const increment = TSL.increment;
200+
export const incrementBefore = TSL.incrementBefore;
197201
export const instance = TSL.instance;
198202
export const instanceIndex = TSL.instanceIndex;
199203
export const instancedArray = TSL.instancedArray;

src/nodes/math/OperatorNode.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { WebGLCoordinateSystem } from '../../constants.js';
22
import TempNode from '../core/TempNode.js';
3-
import { addMethodChaining, int, nodeProxy } from '../tsl/TSLCore.js';
3+
import { addMethodChaining, Fn, int, nodeProxy } from '../tsl/TSLCore.js';
44

55
/**
66
* This node represents basic mathematical and logical operations like addition,
@@ -681,6 +681,68 @@ export const shiftLeft = /*@__PURE__*/ nodeProxy( OperatorNode, '<<' ).setParame
681681
*/
682682
export const shiftRight = /*@__PURE__*/ nodeProxy( OperatorNode, '>>' ).setParameterLength( 2 ).setName( 'shiftRight' );
683683

684+
/**
685+
* Increments a node by 1.
686+
*
687+
* @tsl
688+
* @function
689+
* @param {Node} a - The node to increment.
690+
* @returns {OperatorNode}
691+
*/
692+
export const incrementBefore = Fn( ( [ a ] ) => {
693+
694+
a.addAssign( 1 );
695+
return a;
696+
697+
} );
698+
699+
/**
700+
* Decrements a node by 1.
701+
*
702+
* @tsl
703+
* @function
704+
* @param {Node} a - The node to decrement.
705+
* @returns {OperatorNode}
706+
*/
707+
export const decrementBefore = Fn( ( [ a ] ) => {
708+
709+
a.subAssign( 1 );
710+
return a;
711+
712+
} );
713+
714+
/**
715+
* Increments a node by 1 and returns the previous value.
716+
*
717+
* @tsl
718+
* @function
719+
* @param {Node} a - The node to increment.
720+
* @returns {OperatorNode}
721+
*/
722+
export const increment = /*@__PURE__*/ Fn( ( [ a ] ) => {
723+
724+
const temp = int( a ).toConst();
725+
a.addAssign( 1 );
726+
return temp;
727+
728+
} );
729+
730+
/**
731+
* Decrements a node by 1 and returns the previous value.
732+
*
733+
* @tsl
734+
* @function
735+
* @param {Node} a - The node to decrement.
736+
* @returns {OperatorNode}
737+
*/
738+
export const decrement = /*@__PURE__*/ Fn( ( [ a ] ) => {
739+
740+
const temp = int( a ).toConst();
741+
a.subAssign( 1 );
742+
return temp;
743+
744+
} );
745+
684746
addMethodChaining( 'add', add );
685747
addMethodChaining( 'sub', sub );
686748
addMethodChaining( 'mul', mul );
@@ -703,6 +765,11 @@ addMethodChaining( 'bitXor', bitXor );
703765
addMethodChaining( 'shiftLeft', shiftLeft );
704766
addMethodChaining( 'shiftRight', shiftRight );
705767

768+
addMethodChaining( 'incrementBefore', incrementBefore );
769+
addMethodChaining( 'decrementBefore', decrementBefore );
770+
addMethodChaining( 'increment', increment );
771+
addMethodChaining( 'decrement', decrement );
772+
706773
/**
707774
* @tsl
708775
* @function

0 commit comments

Comments
 (0)