Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion examples/webgpu_compute_cloth.html
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,4 @@

</script>
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions examples/webgpu_compute_points.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import Stats from 'stats-gl';


import { Fn, uniform, instancedArray, float, vec2, vec3, color, instanceIndex } from 'three/tsl';
import { Fn, uniform, instancedArray, float, vec2, color, instanceIndex } from 'three/tsl';

import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

Expand Down Expand Up @@ -76,7 +76,7 @@
const pointerSize = 0.1;
const distanceFromPointer = pointer.sub( position ).length();

particle.assign( distanceFromPointer.lessThanEqual( pointerSize ).select( vec3(), position ) );
particle.assign( distanceFromPointer.lessThanEqual( pointerSize ).select( vec2(), position ) );

} );

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_compute_sort_bitonic.html
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@

} else {

nextAlgo.assign( nextBlockHeight.greaterThan( WORKGROUP_SIZE[ 0 ] * 2 ).select( StepType.DISPERSE_GLOBAL, StepType.DISPERSE_LOCAL ) );
nextAlgo.assign( nextBlockHeight.greaterThan( WORKGROUP_SIZE[ 0 ] * 2 ).select( StepType.DISPERSE_GLOBAL, StepType.DISPERSE_LOCAL ).uniformFlow() );

}

Expand Down
1 change: 1 addition & 0 deletions src/Three.TSL.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ export const uniform = TSL.uniform;
export const uniformArray = TSL.uniformArray;
export const uniformCubeTexture = TSL.uniformCubeTexture;
export const uniformGroup = TSL.uniformGroup;
export const uniformFlow = TSL.uniformFlow;
export const uniformTexture = TSL.uniformTexture;
export const unpremultiplyAlpha = TSL.unpremultiplyAlpha;
export const userData = TSL.userData;
Expand Down
11 changes: 11 additions & 0 deletions src/nodes/core/ContextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ export default ContextNode;
*/
export const context = /*@__PURE__*/ nodeProxy( ContextNode ).setParameterLength( 1, 2 );

/**
* TSL function for defining a uniformFlow context value for a given node.
*
* @tsl
* @function
* @param {Node} node - The node whose dependencies should all execute within a uniform control-flow path.
* @returns {ContextNode}
*/
export const uniformFlow = ( node ) => context( node, { uniformFlow: true } );

/**
* TSL function for defining a name for the context value for a given node.
*
Expand Down Expand Up @@ -162,4 +172,5 @@ export function label( node, name ) {

addMethodChaining( 'context', context );
addMethodChaining( 'label', label );
addMethodChaining( 'uniformFlow', uniformFlow );
addMethodChaining( 'setName', setName );
16 changes: 16 additions & 0 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,22 @@ class NodeBuilder {

}

/**
* Returns the native snippet for a ternary operation. E.g. GLSL would output
* a ternary op as `cond ? x : y` whereas WGSL would output it as `select(y, x, cond)`
*
* @abstract
* @param {string} condSnippet - The condition determining which expression gets resolved.
* @param {string} ifSnippet - The expression to resolve to if the condition is true.
* @param {string} elseSnippet - The expression to resolve to if the condition is false.
* @return {string} The resolved method name.
*/
getTernary( /* condSnippet, ifSnippet, elseSnippet*/ ) {

return null;

}

/**
* Returns a node for the given hash, see {@link NodeBuilder#setHashNode}.
*
Expand Down
26 changes: 22 additions & 4 deletions src/nodes/math/ConditionalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ class ConditionalNode extends Node {

//

const isUniformFlow = builder.context.uniformFlow;

const properties = builder.getNodeProperties( this );
properties.condNode = condNode;
properties.ifNode = ifNode.context( { nodeBlock: ifNode } );
properties.elseNode = elseNode ? elseNode.context( { nodeBlock: elseNode } ) : null;
properties.ifNode = isUniformFlow ? ifNode : ifNode.context( { nodeBlock: ifNode } );
properties.elseNode = elseNode ? ( isUniformFlow ? elseNode : elseNode.context( { nodeBlock: elseNode } ) ) : null;

}

Expand All @@ -140,6 +142,20 @@ class ConditionalNode extends Node {
nodeData.nodeProperty = nodeProperty;

const nodeSnippet = condNode.build( builder, 'bool' );
const isUniformFlow = builder.context.uniformFlow;

if ( isUniformFlow && elseNode !== null ) {

const ifSnippet = ifNode.build( builder, type );
const elseSnippet = elseNode.build( builder, type );

const mathSnippet = builder.getTernary( nodeSnippet, ifSnippet, elseSnippet );

// TODO: If node property already exists return something else

return builder.format( mathSnippet, type, output );

}

builder.addFlowCode( `\n${ builder.tab }if ( ${ nodeSnippet } ) {\n\n` ).addFlowTab();

Expand Down Expand Up @@ -167,7 +183,8 @@ class ConditionalNode extends Node {

}

builder.removeFlowTab().addFlowCode( builder.tab + '\t' + ifSnippet + '\n\n' + builder.tab + '}' );
builder.addLineFlowCode( ifSnippet, ifNode );
builder.removeFlowTab().addFlowCode( '\n\n' + builder.tab + '}' );

if ( elseNode !== null ) {

Expand Down Expand Up @@ -197,7 +214,8 @@ class ConditionalNode extends Node {

}

builder.removeFlowTab().addFlowCode( builder.tab + '\t' + elseSnippet + '\n\n' + builder.tab + '}\n\n' );
builder.addLineFlowCode( elseSnippet, elseNode );
builder.removeFlowTab().addFlowCode( '\n\n' + builder.tab + '}\n\n' );

} else {

Expand Down
14 changes: 14 additions & 0 deletions src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ class GLSLNodeBuilder extends NodeBuilder {

}

/**
* Returns the native snippet for a ternary operation.
*
* @param {string} condSnippet - The condition determining which expression gets resolved.
* @param {string} ifSnippet - The expression to resolve to if the condition is true.
* @param {string} elseSnippet - The expression to resolve to if the condition is false.
* @return {string} The resolved method name.
*/
getTernary( condSnippet, ifSnippet, elseSnippet ) {

return `${condSnippet} ? ${ifSnippet} : ${elseSnippet}`;

}

/**
* Returns the output struct name. Not relevant for GLSL.
*
Expand Down
15 changes: 15 additions & 0 deletions src/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,21 @@ ${ flowData.code }

}

/**
* Returns the native snippet for a ternary operation.
*
* @param {string} condSnippet - The condition determining which expression gets resolved.
* @param {string} ifSnippet - The expression to resolve to if the condition is true.
* @param {string} elseSnippet - The expression to resolve to if the condition is false.
* @return {string} The resolved method name.
*/
getTernary( condSnippet, ifSnippet, elseSnippet ) {

return `select(${elseSnippet}, ${ifSnippet}, ${condSnippet})`;

}


/**
* Returns the WGSL type of the given node data type.
*
Expand Down
Loading