1
1
import type { AnyData } from '../../data/dataTypes.ts' ;
2
2
import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts' ;
3
+ import { snip } from '../../data/snippet.ts' ;
3
4
import type { AnyWgslData , BaseData } from '../../data/wgslTypes.ts' ;
4
5
import { IllegalBufferAccessError } from '../../errors.ts' ;
5
6
import { getExecMode , inCodegenMode , isInsideTgpuFn } from '../../execMode.ts' ;
@@ -11,9 +12,9 @@ import {
11
12
$getNameForward ,
12
13
$gpuValueOf ,
13
14
$internal ,
15
+ $ownSnippet ,
14
16
$repr ,
15
- $runtimeResource ,
16
- $wgslDataType ,
17
+ $resolve ,
17
18
} from '../../shared/symbols.ts' ;
18
19
import { assertExhaustive } from '../../shared/utilityTypes.ts' ;
19
20
import type { LayoutMembership } from '../../tgpuBindGroupLayout.ts' ;
@@ -37,7 +38,7 @@ export interface TgpuBufferUsage<
37
38
readonly usage : TUsage ;
38
39
readonly [ $repr ] : Infer < TData > ;
39
40
40
- [ $gpuValueOf ] ( ctx : ResolutionCtx ) : InferGPU < TData > ;
41
+ readonly [ $gpuValueOf ] : InferGPU < TData > ;
41
42
value : InferGPU < TData > ;
42
43
$ : InferGPU < TData > ;
43
44
@@ -90,10 +91,10 @@ class TgpuFixedBufferImpl<
90
91
SelfResolvable ,
91
92
TgpuFixedBufferUsage < TData > {
92
93
/** Type-token, not available at runtime */
93
- declare public readonly [ $repr ] : Infer < TData > ;
94
- public readonly resourceType = 'buffer-usage' as const ;
95
- public readonly [ $internal ] : { readonly dataType : TData } ;
96
- public readonly [ $getNameForward ] : TgpuBuffer < TData > ;
94
+ declare readonly [ $repr ] : Infer < TData > ;
95
+ readonly resourceType = 'buffer-usage' as const ;
96
+ readonly [ $internal ] : { readonly dataType : TData } ;
97
+ readonly [ $getNameForward ] : TgpuBuffer < TData > ;
97
98
98
99
constructor (
99
100
public readonly usage : TUsage ,
@@ -108,21 +109,20 @@ class TgpuFixedBufferImpl<
108
109
return this ;
109
110
}
110
111
111
- '~resolve' ( ctx : ResolutionCtx ) : string {
112
+ [ $resolve ] ( ctx : ResolutionCtx ) : string {
113
+ const dataType = this . buffer . dataType ;
112
114
const id = ctx . getUniqueName ( this ) ;
113
115
const { group, binding } = ctx . allocateFixedEntry (
114
116
this . usage === 'uniform'
115
- ? { uniform : this . buffer . dataType }
116
- : { storage : this . buffer . dataType , access : this . usage } ,
117
+ ? { uniform : dataType }
118
+ : { storage : dataType , access : this . usage } ,
117
119
this . buffer ,
118
120
) ;
119
- const usage = usageToVarTemplateMap [ this . usage ] ;
121
+ const usageTemplate = usageToVarTemplateMap [ this . usage ] ;
120
122
121
123
ctx . addDeclaration (
122
- `@group(${ group } ) @binding(${ binding } ) var<${ usage } > ${ id } : ${
123
- ctx . resolve (
124
- this . buffer . dataType ,
125
- )
124
+ `@group(${ group } ) @binding(${ binding } ) var<${ usageTemplate } > ${ id } : ${
125
+ ctx . resolve ( dataType )
126
126
} ;`,
127
127
) ;
128
128
@@ -133,17 +133,18 @@ class TgpuFixedBufferImpl<
133
133
return `${ this . usage } :${ getName ( this ) ?? '<unnamed>' } ` ;
134
134
}
135
135
136
- [ $gpuValueOf ] ( ) : InferGPU < TData > {
137
- return new Proxy (
138
- {
139
- [ $internal ] : true ,
140
- [ $runtimeResource ] : true ,
141
- [ $wgslDataType ] : this . buffer . dataType ,
142
- '~resolve' : ( ctx : ResolutionCtx ) => ctx . resolve ( this ) ,
143
- toString : ( ) => `.value: ${ getName ( this ) ?? '<unnamed>' } ` ,
136
+ get [ $gpuValueOf ] ( ) : InferGPU < TData > {
137
+ const dataType = this . buffer . dataType ;
138
+ const name = getName ( this ) ;
139
+
140
+ return new Proxy ( {
141
+ [ $internal ] : true ,
142
+ get [ $ownSnippet ] ( ) {
143
+ return snip ( this , dataType ) ;
144
144
} ,
145
- valueProxyHandler ,
146
- ) as InferGPU < TData > ;
145
+ [ $resolve ] : ( ctx ) => ctx . resolve ( this ) ,
146
+ toString : ( ) => `${ this . usage } :${ name ?? '<unnamed>' } .$` ,
147
+ } , valueProxyHandler ) as InferGPU < TData > ;
147
148
}
148
149
149
150
get $ ( ) : InferGPU < TData > {
@@ -161,7 +162,7 @@ class TgpuFixedBufferImpl<
161
162
}
162
163
163
164
if ( mode . type === 'codegen' ) {
164
- return this [ $gpuValueOf ] ( ) ;
165
+ return this [ $gpuValueOf ] ;
165
166
}
166
167
167
168
if ( mode . type === 'simulate' ) {
@@ -219,26 +220,28 @@ export class TgpuLaidOutBufferImpl<
219
220
TUsage extends BindableBufferUsage ,
220
221
> implements TgpuBufferUsage < TData , TUsage > , SelfResolvable {
221
222
/** Type-token, not available at runtime */
222
- declare public readonly [ $repr ] : Infer < TData > ;
223
- public readonly resourceType = 'buffer-usage' as const ;
224
- public readonly [ $internal ] : { readonly dataType : TData } ;
223
+ declare readonly [ $repr ] : Infer < TData > ;
224
+ readonly [ $internal ] : { readonly dataType : TData } ;
225
+ readonly resourceType = 'buffer-usage' as const ;
226
+ readonly #membership: LayoutMembership ;
225
227
226
228
constructor (
227
229
public readonly usage : TUsage ,
228
230
public readonly dataType : TData ,
229
- private readonly _membership : LayoutMembership ,
231
+ membership : LayoutMembership ,
230
232
) {
231
233
this [ $internal ] = { dataType } ;
232
- setName ( this , _membership . key ) ;
234
+ this . #membership = membership ;
235
+ setName ( this , membership . key ) ;
233
236
}
234
237
235
- '~ resolve' ( ctx : ResolutionCtx ) : string {
238
+ [ $ resolve] ( ctx : ResolutionCtx ) : string {
236
239
const id = ctx . getUniqueName ( this ) ;
237
- const group = ctx . allocateLayoutEntry ( this . _membership . layout ) ;
240
+ const group = ctx . allocateLayoutEntry ( this . #membership . layout ) ;
238
241
const usage = usageToVarTemplateMap [ this . usage ] ;
239
242
240
243
ctx . addDeclaration (
241
- `@group(${ group } ) @binding(${ this . _membership . idx } ) var<${ usage } > ${ id } : ${
244
+ `@group(${ group } ) @binding(${ this . #membership . idx } ) var<${ usage } > ${ id } : ${
242
245
ctx . resolve ( this . dataType )
243
246
} ;`,
244
247
) ;
@@ -250,22 +253,22 @@ export class TgpuLaidOutBufferImpl<
250
253
return `${ this . usage } :${ getName ( this ) ?? '<unnamed>' } ` ;
251
254
}
252
255
253
- [ $gpuValueOf ] ( ) : InferGPU < TData > {
254
- return new Proxy (
255
- {
256
- [ $internal ] : true ,
257
- [ $runtimeResource ] : true ,
258
- [ $wgslDataType ] : this . dataType ,
259
- '~resolve' : ( ctx : ResolutionCtx ) => ctx . resolve ( this ) ,
260
- toString : ( ) => `.value:${ getName ( this ) ?? '<unnamed>' } ` ,
256
+ get [ $gpuValueOf ] ( ) : InferGPU < TData > {
257
+ const schema = this . dataType as unknown as AnyData ;
258
+
259
+ return new Proxy ( {
260
+ [ $internal ] : true ,
261
+ get [ $ownSnippet ] ( ) {
262
+ return snip ( this , schema ) ;
261
263
} ,
262
- valueProxyHandler ,
263
- ) as InferGPU < TData > ;
264
+ [ $resolve ] : ( ctx ) => ctx . resolve ( this ) ,
265
+ toString : ( ) => `${ this . usage } :${ getName ( this ) ?? '<unnamed>' } .$` ,
266
+ } , valueProxyHandler ) as InferGPU < TData > ;
264
267
}
265
268
266
269
get $ ( ) : InferGPU < TData > {
267
270
if ( inCodegenMode ( ) ) {
268
- return this [ $gpuValueOf ] ( ) ;
271
+ return this [ $gpuValueOf ] ;
269
272
}
270
273
271
274
throw new Error (
0 commit comments