Skip to content

Commit ecd120d

Browse files
authored
TSL: Fix and improve bvec operators (#30922)
* fix bvec operators * Update OperatorNode.js * revision * rev * rev
1 parent f6f0183 commit ecd120d

File tree

2 files changed

+40
-77
lines changed

2 files changed

+40
-77
lines changed

src/nodes/core/NodeBuilder.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2670,7 +2670,9 @@ class NodeBuilder {
26702670

26712671
if ( fromTypeLength > toTypeLength ) {
26722672

2673-
return this.format( `${ snippet }.${ 'xyz'.slice( 0, toTypeLength ) }`, this.getTypeFromLength( toTypeLength, this.getComponentType( fromType ) ), toType );
2673+
snippet = toType === 'bool' ? `all( ${ snippet } )` : `${ snippet }.${ 'xyz'.slice( 0, toTypeLength ) }`;
2674+
2675+
return this.format( snippet, this.getTypeFromLength( toTypeLength, this.getComponentType( fromType ) ), toType );
26742676

26752677
}
26762678

src/nodes/math/OperatorNode.js

Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { WebGLCoordinateSystem } from '../../constants.js';
22
import TempNode from '../core/TempNode.js';
33
import { addMethodChaining, Fn, int, nodeProxy } from '../tsl/TSLCore.js';
44

5+
const _vectorOperators = {
6+
'==': 'equal',
7+
'!=': 'notEqual',
8+
'<': 'lessThan',
9+
'>': 'greaterThan',
10+
'<=': 'lessThanEqual',
11+
'>=': 'greaterThanEqual',
12+
'%': 'mod'
13+
};
14+
515
/**
616
* This node represents basic mathematical and logical operations like addition,
717
* subtraction or comparisons (e.g. `equal()`).
@@ -75,15 +85,27 @@ class OperatorNode extends TempNode {
7585

7686
}
7787

88+
/**
89+
* Returns the operator method name.
90+
*
91+
* @param {NodeBuilder} builder - The current node builder.
92+
* @param {string} output - The output type.
93+
* @returns {string} The operator method name.
94+
*/
95+
getOperatorMethod( builder, output ) {
96+
97+
return builder.getMethod( _vectorOperators[ this.op ], output );
98+
99+
}
100+
78101
/**
79102
* This method is overwritten since the node type is inferred from the operator
80103
* and the input node types.
81104
*
82105
* @param {NodeBuilder} builder - The current node builder.
83-
* @param {string} output - The current output string.
84106
* @return {string} The node type.
85107
*/
86-
getNodeType( builder, output ) {
108+
getNodeType( builder ) {
87109

88110
const op = this.op;
89111

@@ -105,13 +127,13 @@ class OperatorNode extends TempNode {
105127

106128
return builder.getIntegerType( typeA );
107129

108-
} else if ( op === '!' || op === '==' || op === '!=' || op === '&&' || op === '||' || op === '^^' ) {
130+
} else if ( op === '!' || op === '&&' || op === '||' || op === '^^' ) {
109131

110132
return 'bool';
111133

112-
} else if ( op === '<' || op === '>' || op === '<=' || op === '>=' ) {
134+
} else if ( op === '==' || op === '!=' || op === '<' || op === '>' || op === '<=' || op === '>=' ) {
113135

114-
const typeLength = output ? builder.getTypeLength( output ) : Math.max( builder.getTypeLength( typeA ), builder.getTypeLength( typeB ) );
136+
const typeLength = Math.max( builder.getTypeLength( typeA ), builder.getTypeLength( typeB ) );
115137

116138
return typeLength > 1 ? `bvec${ typeLength }` : 'bool';
117139

@@ -172,7 +194,7 @@ class OperatorNode extends TempNode {
172194
const aNode = this.aNode;
173195
const bNode = this.bNode;
174196

175-
const type = this.getNodeType( builder, output );
197+
const type = this.getNodeType( builder );
176198

177199
let typeA = null;
178200
let typeB = null;
@@ -188,6 +210,10 @@ class OperatorNode extends TempNode {
188210

189211
typeB = typeA;
190212

213+
} else if ( builder.isVector( typeB ) ) {
214+
215+
typeA = typeB;
216+
191217
} else if ( typeA !== typeB ) {
192218

193219
typeA = typeB = 'float';
@@ -264,20 +290,19 @@ class OperatorNode extends TempNode {
264290
const a = aNode.build( builder, typeA );
265291
const b = typeof bNode !== 'undefined' ? bNode.build( builder, typeB ) : null;
266292

267-
const outputLength = builder.getTypeLength( output );
268293
const fnOpSnippet = builder.getFunctionOperator( op );
269294

270295
if ( output !== 'void' ) {
271296

272297
const isGLSL = builder.renderer.coordinateSystem === WebGLCoordinateSystem;
273298

274-
if ( op === '==' ) {
299+
if ( op === '==' || op === '!=' || op === '<' || op === '>' || op === '<=' || op === '>=' ) {
275300

276301
if ( isGLSL ) {
277302

278-
if ( outputLength > 1 ) {
303+
if ( builder.isVector( typeA ) ) {
279304

280-
return builder.format( `${ builder.getMethod( 'equal', output ) }( ${ a }, ${ b } )`, type, output );
305+
return builder.format( `${ this.getOperatorMethod( builder, output ) }( ${ a }, ${ b } )`, type, output );
281306

282307
} else {
283308

@@ -289,71 +314,7 @@ class OperatorNode extends TempNode {
289314

290315
// WGSL
291316

292-
if ( outputLength > 1 || ! builder.isVector( typeA ) ) {
293-
294-
return builder.format( `( ${ a } == ${ b } )`, type, output );
295-
296-
} else {
297-
298-
return builder.format( `all( ${ a } == ${ b } )`, type, output );
299-
300-
}
301-
302-
}
303-
304-
} else if ( op === '<' && outputLength > 1 ) {
305-
306-
if ( isGLSL ) {
307-
308-
return builder.format( `${ builder.getMethod( 'lessThan', output ) }( ${ a }, ${ b } )`, type, output );
309-
310-
} else {
311-
312-
// WGSL
313-
314-
return builder.format( `( ${ a } < ${ b } )`, type, output );
315-
316-
}
317-
318-
} else if ( op === '<=' && outputLength > 1 ) {
319-
320-
if ( isGLSL ) {
321-
322-
return builder.format( `${ builder.getMethod( 'lessThanEqual', output ) }( ${ a }, ${ b } )`, type, output );
323-
324-
} else {
325-
326-
// WGSL
327-
328-
return builder.format( `( ${ a } <= ${ b } )`, type, output );
329-
330-
}
331-
332-
} else if ( op === '>' && outputLength > 1 ) {
333-
334-
if ( isGLSL ) {
335-
336-
return builder.format( `${ builder.getMethod( 'greaterThan', output ) }( ${ a }, ${ b } )`, type, output );
337-
338-
} else {
339-
340-
// WGSL
341-
342-
return builder.format( `( ${ a } > ${ b } )`, type, output );
343-
344-
}
345-
346-
} else if ( op === '>=' && outputLength > 1 ) {
347-
348-
if ( isGLSL ) {
349-
350-
return builder.format( `${ builder.getMethod( 'greaterThanEqual', output ) }( ${ a }, ${ b } )`, type, output );
351-
352-
} else {
353-
354-
// WGSL
355-
356-
return builder.format( `( ${ a } >= ${ b } )`, type, output );
317+
return builder.format( `( ${ a } ${ op } ${ b } )`, type, output );
357318

358319
}
359320

@@ -365,7 +326,7 @@ class OperatorNode extends TempNode {
365326

366327
} else {
367328

368-
return builder.format( `${ builder.getMethod( 'mod', type ) }( ${ a }, ${ b } )`, type, output );
329+
return builder.format( `${ this.getOperatorMethod( builder, type ) }( ${ a }, ${ b } )`, type, output );
369330

370331
}
371332

0 commit comments

Comments
 (0)