@@ -19,7 +19,12 @@ import { $gpuCallable, $internal, $providing, isMarkedInternal } from '../shared
1919import { safeStringify } from '../shared/stringify.ts' ;
2020import { pow } from '../std/numeric.ts' ;
2121import { add , div , mul , neg , sub } from '../std/operators.ts' ;
22- import { isGPUCallable , isKnownAtComptime , type DualFn } from '../types.ts' ;
22+ import {
23+ isGPUCallable ,
24+ isKnownAtComptime ,
25+ type BindableBufferUsage ,
26+ type DualFn ,
27+ } from '../types.ts' ;
2328import { convertStructValues , convertToCommonType , tryConvertSnippet } from './conversion.ts' ;
2429import {
2530 ArrayExpression ,
@@ -44,10 +49,14 @@ import type { ExternalMap } from '../core/resolve/externals.ts';
4449import * as forOfUtils from './forOfUtils.ts' ;
4550import { isTgpuRange } from '../std/range.ts' ;
4651import { stringifyNode } from '../shared/tseynit.ts' ;
47- import type { FunctionDefinitionOptions } from './shaderGenerator_members.ts' ;
52+ import type {
53+ FunctionDefinitionOptions ,
54+ VariableDefinitionOptions ,
55+ } from './shaderGenerator_members.ts' ;
4856import { getAttributesString } from '../data/attributes.ts' ;
4957import { validSelectBranchTypes } from '../std/boolean.ts' ;
5058import { isInfixDispatch } from './infixDispatch.ts' ;
59+ import type { VariableScope } from '../core/variable/tgpuVariable.ts' ;
5160
5261const { NodeTypeCatalog : NODE } = tinyest ;
5362
@@ -193,6 +202,14 @@ const binaryOpCodeToCodegen = {
193202 '**' : pow [ $gpuCallable ] . call . bind ( pow ) ,
194203} satisfies Partial < Record < tinyest . BinaryOperator , ( ...args : never [ ] ) => unknown > > ;
195204
205+ const usageToVarTemplateMap : Record < VariableScope | BindableBufferUsage , string > = {
206+ private : 'private' ,
207+ workgroup : 'workgroup' ,
208+ uniform : 'uniform' ,
209+ mutable : 'storage, read_write' ,
210+ readonly : 'storage, read' ,
211+ } ;
212+
196213export class WgslGenerator implements ShaderGenerator {
197214 #ctx: GenerationCtx | undefined = undefined ;
198215 // used to detect `continue` and `break` nodes in loop body
@@ -844,6 +861,30 @@ ${this.ctx.pre}}`;
844861 assertExhaustive ( expression ) ;
845862 }
846863
864+ public globalConstDefinition ( id : string , schema : wgsl . BaseData , init : Snippet ) : string {
865+ const resolvedDataType = this . ctx . resolve ( schema ) . value ;
866+ const resolvedValue = this . ctx . resolveSnippet ( init ) . value ;
867+
868+ return `const ${ id } : ${ resolvedDataType } = ${ resolvedValue } ;` ;
869+ }
870+
871+ public globalVarDefinition ( options : VariableDefinitionOptions ) : string {
872+ let pre = `var<${ usageToVarTemplateMap [ options . scope ] } > ${ options . name } : ${ this . ctx . resolve ( options . dataType ) . value } ` ;
873+
874+ if ( options . binding !== undefined ) {
875+ pre = `@binding(${ options . binding } ) ` + pre ;
876+ }
877+
878+ if ( options . group !== undefined ) {
879+ pre = `@group(${ options . group } ) ` + pre ;
880+ }
881+
882+ if ( options . init ) {
883+ return `${ pre } = ${ this . ctx . resolveSnippet ( options . init ) . value } ;` ;
884+ }
885+ return `${ pre } ;` ;
886+ }
887+
847888 public functionDefinition ( options : FunctionDefinitionOptions ) : string {
848889 // Function body
849890 let body = this . _block ( options . body ) ;
0 commit comments