From 97d4621e962a79f1ecec39e5e509316008a6bc24 Mon Sep 17 00:00:00 2001 From: sunag Date: Fri, 3 Apr 2026 00:02:39 -0300 Subject: [PATCH] WGSLNodeBuilder: Add `allowGlobalVariables` (#33319) --- src/nodes/gpgpu/BarrierNode.js | 9 +++++++-- src/renderers/webgpu/nodes/WGSLNodeBuilder.js | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/nodes/gpgpu/BarrierNode.js b/src/nodes/gpgpu/BarrierNode.js index 56c1ee0cdff872..33341e413aed4c 100644 --- a/src/nodes/gpgpu/BarrierNode.js +++ b/src/nodes/gpgpu/BarrierNode.js @@ -25,13 +25,18 @@ class BarrierNode extends Node { } + setup( builder ) { + + builder.allowEarlyReturns = false; + builder.allowGlobalVariables = false; + + } + generate( builder ) { const { scope } = this; const { renderer } = builder; - builder.allowEarlyReturns = false; - if ( renderer.backend.isWebGLBackend === true ) { builder.addFlowCode( `\t// ${scope}Barrier \n` ); diff --git a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js index be213638ce2f3f..45dc7fef7d8d22 100644 --- a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +++ b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js @@ -238,6 +238,14 @@ class WGSLNodeBuilder extends NodeBuilder { */ this.allowEarlyReturns = true; + /** + * A flag that indicates that global variables are allowed. + * + * @type {boolean} + * @default true + */ + this.allowGlobalVariables = true; + } /** @@ -2081,12 +2089,14 @@ ${ flowData.code } this.shaderStage = shaderStage; + const allowGlobal = this.allowGlobalVariables; + const stageData = shadersData[ shaderStage ]; stageData.uniforms = this.getUniforms( shaderStage ); stageData.attributes = this.getAttributes( shaderStage ); stageData.varyings = this.getVaryings( shaderStage ); stageData.structs = this.getStructs( shaderStage ); - stageData.vars = this.getVars( shaderStage, true ); + stageData.vars = this.getVars( shaderStage, allowGlobal ); stageData.codes = this.getCodes( shaderStage ); stageData.directives = this.getDirectives( shaderStage ); stageData.scopedArrays = this.getScopedArrays( shaderStage ); @@ -2445,7 +2455,7 @@ ${ shaderData.structs } ${ shaderData.uniforms } // vars -${ shaderData.vars } +${ this.allowGlobalVariables ? shaderData.vars : '' } // codes ${ shaderData.codes } @@ -2453,6 +2463,9 @@ ${ shaderData.codes } @compute @workgroup_size( ${ workgroupSizeX }, ${ workgroupSizeY }, ${ workgroupSizeZ } ) fn main( ${ shaderData.attributes } ) { + // local vars + ${ this.allowGlobalVariables ? '' : shaderData.vars } + // system instanceIndex = globalId.x + globalId.y * ( ${ workgroupSizeX } * numWorkgroups.x )