@@ -20,7 +20,12 @@ import { $gpuCallable, $internal, $providing, isMarkedInternal } from '../shared
2020import { safeStringify } from '../shared/stringify.ts' ;
2121import { pow } from '../std/numeric.ts' ;
2222import { add , div , mul , neg , sub } from '../std/operators.ts' ;
23- import { isGPUCallable , isKnownAtComptime , type DualFn } from '../types.ts' ;
23+ import {
24+ isGPUCallable ,
25+ isKnownAtComptime ,
26+ type BindableBufferUsage ,
27+ type DualFn ,
28+ } from '../types.ts' ;
2429import { convertStructValues , convertToCommonType , tryConvertSnippet } from './conversion.ts' ;
2530import {
2631 ArrayExpression ,
@@ -45,8 +50,12 @@ import type { ExternalMap } from '../core/resolve/externals.ts';
4550import * as forOfUtils from './forOfUtils.ts' ;
4651import { isTgpuRange } from '../std/range.ts' ;
4752import { stringifyNode } from '../shared/tseynit.ts' ;
48- import type { FunctionDefinitionOptions } from './shaderGenerator_members.ts' ;
53+ import type {
54+ FunctionDefinitionOptions ,
55+ VariableDefinitionOptions ,
56+ } from './shaderGenerator_members.ts' ;
4957import { getAttributesString } from '../data/attributes.ts' ;
58+ import type { VariableScope } from '../core/variable/tgpuVariable.ts' ;
5059
5160const { NodeTypeCatalog : NODE } = tinyest ;
5261
@@ -192,6 +201,14 @@ const binaryOpCodeToCodegen = {
192201 '**' : pow [ $gpuCallable ] . call . bind ( pow ) ,
193202} satisfies Partial < Record < tinyest . BinaryOperator , ( ...args : never [ ] ) => unknown > > ;
194203
204+ const usageToVarTemplateMap : Record < VariableScope | BindableBufferUsage , string > = {
205+ private : 'private' ,
206+ workgroup : 'workgroup' ,
207+ uniform : 'uniform' ,
208+ mutable : 'storage, read_write' ,
209+ readonly : 'storage, read' ,
210+ } ;
211+
195212export class WgslGenerator implements ShaderGenerator {
196213 #ctx: GenerationCtx | undefined = undefined ;
197214 // used to detect `continue` and `break` nodes in loop body
@@ -876,6 +893,30 @@ ${this.ctx.pre}}`;
876893 assertExhaustive ( expression ) ;
877894 }
878895
896+ public globalConstDefinition ( id : string , schema : wgsl . BaseData , init : Snippet ) : string {
897+ const resolvedDataType = this . ctx . resolve ( schema ) . value ;
898+ const resolvedValue = this . ctx . resolveSnippet ( init ) . value ;
899+
900+ return `const ${ id } : ${ resolvedDataType } = ${ resolvedValue } ;` ;
901+ }
902+
903+ public globalVarDefinition ( options : VariableDefinitionOptions ) : string {
904+ let pre = `var<${ usageToVarTemplateMap [ options . scope ] } > ${ options . name } : ${ this . ctx . resolve ( options . dataType ) . value } ` ;
905+
906+ if ( options . binding !== undefined ) {
907+ pre = `@binding(${ options . binding } ) ` + pre ;
908+ }
909+
910+ if ( options . group !== undefined ) {
911+ pre = `@group(${ options . group } ) ` + pre ;
912+ }
913+
914+ if ( options . init ) {
915+ return `${ pre } = ${ this . ctx . resolveSnippet ( options . init ) . value } ;` ;
916+ }
917+ return `${ pre } ;` ;
918+ }
919+
879920 public functionDefinition ( options : FunctionDefinitionOptions ) : string {
880921 // Function body
881922 let body = this . _block ( options . body ) ;
0 commit comments