Skip to content

Commit 3643961

Browse files
authored
TSL: Add global and local scope (#33302)
1 parent 0510c36 commit 3643961

3 files changed

Lines changed: 36 additions & 52 deletions

File tree

src/nodes/core/NodeBuilder.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,25 +2672,26 @@ class NodeBuilder {
26722672
* Returns the variable definitions as a shader string for the given shader stage.
26732673
*
26742674
* @param {('vertex'|'fragment'|'compute'|'any')} shaderStage - The shader stage.
2675+
* @param {boolean} [global=false] - Whether the variables are global.
26752676
* @return {string} The variable code section.
26762677
*/
2677-
getVars( shaderStage ) {
2678+
getVars( shaderStage, global = false ) {
26782679

2679-
let snippet = '';
2680+
const snippets = [];
26802681

26812682
const vars = this.vars[ shaderStage ];
26822683

26832684
if ( vars !== undefined ) {
26842685

26852686
for ( const variable of vars ) {
26862687

2687-
snippet += `${ this.getVar( variable.type, variable.name ) }; `;
2688+
snippets.push( `${ this.getVar( variable.type, variable.name, variable.count ) };` );
26882689

26892690
}
26902691

26912692
}
26922693

2693-
return snippet;
2694+
return snippets.join( global ? '\n' : '\n\t' );
26942695

26952696
}
26962697

src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -665,32 +665,6 @@ ${ flowData.code }
665665

666666
}
667667

668-
/**
669-
* Returns the variables of the given shader stage as a GLSL string.
670-
*
671-
* @param {string} shaderStage - The shader stage.
672-
* @return {string} The GLSL snippet that defines the variables.
673-
*/
674-
getVars( shaderStage ) {
675-
676-
const snippets = [];
677-
678-
const vars = this.vars[ shaderStage ];
679-
680-
if ( vars !== undefined ) {
681-
682-
for ( const variable of vars ) {
683-
684-
snippets.push( `${ this.getVar( variable.type, variable.name, variable.count ) };` );
685-
686-
}
687-
688-
}
689-
690-
return snippets.join( '\n\t' );
691-
692-
}
693-
694668
/**
695669
* Returns the uniforms of the given shader stage as a GLSL string.
696670
*
@@ -1425,14 +1399,14 @@ ${shaderData.varyings}
14251399
// attributes
14261400
${shaderData.attributes}
14271401
1402+
// vars
1403+
${shaderData.vars}
1404+
14281405
// codes
14291406
${shaderData.codes}
14301407
14311408
void main() {
14321409
1433-
// vars
1434-
${shaderData.vars}
1435-
14361410
// transforms
14371411
${shaderData.transforms}
14381412
@@ -1474,14 +1448,14 @@ ${shaderData.uniforms}
14741448
// varyings
14751449
${shaderData.varyings}
14761450
1451+
// vars
1452+
${shaderData.vars}
1453+
14771454
// codes
14781455
${shaderData.codes}
14791456
14801457
void main() {
14811458
1482-
// vars
1483-
${shaderData.vars}
1484-
14851459
// flow
14861460
${shaderData.flow}
14871461
@@ -1552,7 +1526,7 @@ void main() {
15521526
stageData.uniforms = this.getUniforms( shaderStage );
15531527
stageData.attributes = this.getAttributes( shaderStage );
15541528
stageData.varyings = this.getVaryings( shaderStage );
1555-
stageData.vars = this.getVars( shaderStage );
1529+
stageData.vars = this.getVars( shaderStage, true );
15561530
stageData.structs = this.getStructs( shaderStage );
15571531
stageData.codes = this.getCodes( shaderStage );
15581532
stageData.transforms = this.getTransforms( shaderStage );

src/renderers/webgpu/nodes/WGSLNodeBuilder.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,11 +1741,12 @@ ${ flowData.code }
17411741
* @param {string} type - The variable's type.
17421742
* @param {string} name - The variable's name.
17431743
* @param {?number} [count=null] - The array length.
1744+
* @param {string} [qualifier=''] - The variable's qualifier.
17441745
* @return {string} The WGSL snippet that defines a variable.
17451746
*/
1746-
getVar( type, name, count = null ) {
1747+
getVar( type, name, count = null, qualifier = '' ) {
17471748

1748-
let snippet = `var ${ name } : `;
1749+
let snippet = `var${ qualifier } ${ name } : `;
17491750

17501751
if ( count !== null ) {
17511752

@@ -1767,7 +1768,15 @@ ${ flowData.code }
17671768
* @param {string} shaderStage - The shader stage.
17681769
* @return {string} The WGSL snippet that defines the variables.
17691770
*/
1770-
getVars( shaderStage ) {
1771+
getVars( shaderStage, global = false ) {
1772+
1773+
let qualifier = '';
1774+
1775+
if ( global ) {
1776+
1777+
qualifier = '<private>';
1778+
1779+
}
17711780

17721781
const snippets = [];
17731782
const vars = this.vars[ shaderStage ];
@@ -1776,13 +1785,13 @@ ${ flowData.code }
17761785

17771786
for ( const variable of vars ) {
17781787

1779-
snippets.push( `\t${ this.getVar( variable.type, variable.name, variable.count ) };` );
1788+
snippets.push( `${ this.getVar( variable.type, variable.name, variable.count, qualifier ) };` );
17801789

17811790
}
17821791

17831792
}
17841793

1785-
return `\n${ snippets.join( '\n' ) }\n`;
1794+
return global ? snippets.join( '\n' ) : `\n\t${ snippets.join( '\n\t' ) }\n`;
17861795

17871796
}
17881797

@@ -2077,7 +2086,7 @@ ${ flowData.code }
20772086
stageData.attributes = this.getAttributes( shaderStage );
20782087
stageData.varyings = this.getVaryings( shaderStage );
20792088
stageData.structs = this.getStructs( shaderStage );
2080-
stageData.vars = this.getVars( shaderStage );
2089+
stageData.vars = this.getVars( shaderStage, true );
20812090
stageData.codes = this.getCodes( shaderStage );
20822091
stageData.directives = this.getDirectives( shaderStage );
20832092
stageData.scopedArrays = this.getScopedArrays( shaderStage );
@@ -2352,15 +2361,15 @@ ${shaderData.uniforms}
23522361
${shaderData.varyings}
23532362
var<private> varyings : VaryingsStruct;
23542363
2364+
// vars
2365+
${shaderData.vars}
2366+
23552367
// codes
23562368
${shaderData.codes}
23572369
23582370
@vertex
23592371
fn main( ${shaderData.attributes} ) -> VaryingsStruct {
23602372
2361-
// vars
2362-
${shaderData.vars}
2363-
23642373
// flow
23652374
${shaderData.flow}
23662375
@@ -2390,15 +2399,15 @@ ${shaderData.structs}
23902399
// uniforms
23912400
${shaderData.uniforms}
23922401
2402+
// vars
2403+
${shaderData.vars}
2404+
23932405
// codes
23942406
${shaderData.codes}
23952407
23962408
@fragment
23972409
fn main( ${shaderData.varyings} ) -> ${shaderData.returnType} {
23982410
2399-
// vars
2400-
${shaderData.vars}
2401-
24022411
// flow
24032412
${shaderData.flow}
24042413
@@ -2435,6 +2444,9 @@ ${ shaderData.structs }
24352444
// uniforms
24362445
${ shaderData.uniforms }
24372446
2447+
// vars
2448+
${ shaderData.vars }
2449+
24382450
// codes
24392451
${ shaderData.codes }
24402452
@@ -2446,9 +2458,6 @@ fn main( ${ shaderData.attributes } ) {
24462458
+ globalId.y * ( ${ workgroupSizeX } * numWorkgroups.x )
24472459
+ globalId.z * ( ${ workgroupSizeX } * numWorkgroups.x ) * ( ${ workgroupSizeY } * numWorkgroups.y );
24482460
2449-
// vars
2450-
${ shaderData.vars }
2451-
24522461
// flow
24532462
${ shaderData.flow }
24542463

0 commit comments

Comments
 (0)