You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To get there, resolution has to remember which stage each binding was used from. The catch is memoization:
items are cached per slot values they read, wobble reads none, so it's resolved once
vertex resolves wobble (miss), time gets recorded with vertex
fragment resolves wobble (hit), the cached snippet comes back and nothing gets recorded
time ends up visible only to vertex, and the fragment shader fails validation
Anything an item records into the context (not into the WGSL) has this problem. #3310 solves it for bindings specifically:
usedFixedBindings on every item layer of ItemStateStack
a walk over item and slot-binding layers to tell where the stage came from
a replay loop in the cache-hit branch of _getOrInstantiate
mutable stages / usedOutsideStages on FixedBindingConfig
#3220 moves those layers into SlotScope, which is shared with the CPU path, so none of this has a home there. The next thing that needs to survive caching would need its own copy
Probes
Every resolved item gets a set of payloads per probe, cached next to its snippet. Whoever resolves the item merges those sets into its own, on a hit and on a miss
Types (core/resolve/probe.ts)
// typed key, compared by identityclassProbe<TPayload>{declarereadonly[$repr]: TPayload;}// read-only view of what an item reported (itself or through what it resolved)interfaceProbeSets{get<T>(probe: Probe<T>): ReadonlySet<T>;entries(): Iterable<[Probe<unknown>,ReadonlySet<unknown>]>;}// per item being resolved: sets + a stack of active maps per probeclassProbeFrameimplementsProbeSets{report<T>(probe: Probe<T>,payload: T): void;withMap<T,R>(probe: Probe<T>,map: (payload: T)=>T,callback: ()=>R): R;}
Context (ResolutionCtx)
report<T>(probe: Probe<T>,payload: T): void;withProbeMap<T,R>(probe: Probe<T>,map: (payload: T)=>T,callback: ()=>R): R;withShaderStage<T>(stage: ShaderStage,callback: ()=>T): T;getprobes(): ProbeSets;// root frame, read by resolve() at the end
Rules
miss: resolving an item pushes a new frame, report adds to the top one
report: the payload goes through the frame's active maps for that probe (innermost first), then into the set
cache: the frame is stored with the result, { slotToValueMap, result, probes }
merge: when ctx.resolve(item) returns, hit or miss, the item's payloads are re-reported into the caller's frame, so they pass the caller's maps
root: the context starts with a root frame, resolve() reads it once everything is resolved
Properties
hits and misses take the same merge path, resolving something twice gives the same sets as once
a map is local to its frame, so a cached item stores only what it saw itself, a helper keeps stage-free payloads and works for any caller
maps have to be pure and idempotent, since a payload can pass the same kind of map more than once
payloads are compared by identity (plain Set), producers that combine values intern them
sets allocate on the first report, an item that reports nothing caches an empty frame
lazies (memoizedLazy) don't get frames, nothing can report while a lazy computes (bindings are allocated when a buffer use is resolved)
plain 'use gpu' functions resolved directly aren't memoized and get no frame, the shellless instance they resolve to has one
no user-facing API changes, ResolutionCtx (exported from typegpu/~internal) gains report, withProbeMap and withShaderStage
typeBindingUse={readonlybinding: FixedBindingConfig;readonlystage: ShaderStage|undefined;};constbindingUseProbe=newProbe<BindingUse>();// interned per (binding, stage) with WeakMemo, so identity dedupesfunctionbindingUse(binding: FixedBindingConfig,stage?: ShaderStage): BindingUse;
withShaderStage replaces ctx.withSlots([[shaderStageSlot, stage]], ...) at the 5 entry sites (tgpuComputeFn, tgpuVertexFn, tgpuFragmentFn, autoIO x2):
withShaderStage(stage,callback){returnthis.withSlots([[shaderStageSlot,stage]],()=>this.withProbeMap(bindingUseProbe,(use)=>(use.stage ? use : bindingUse(use.binding,stage)),callback,),);}
Reading (fixedLayoutEntries, called from resolve())
group the root set's uses by binding
any use with stage: undefined (used outside an entry) → keep the layout entry as is, visible everywhere like before
otherwise → visibility from the used stages, in ALL_STAGES order (compute, vertex, fragment)
The example again
vertex miss, map(vertex)
wobble miss, reports {time, undefined}, cached as such
→ {time, vertex}
fragment miss, map(fragment)
wobble hit, cached {time, undefined}
→ {time, fragment}
root time → ['vertex', 'fragment']
frames live in ResolutionCtxImpl, pushed inside the compute callback of slotScope.getOrInstantiate
the cached value carries the probe sets, InstanceMap<TKey, T> already allows that
SlotScope stays untouched, whichever lands second only reconciles the cache entry shape
Tests
#3310's binding visibility tests stay as they are and pass unchanged, plus one for plain functions resolved by an entry. Dropping the merge on the hit path fails the vertex + fragment test (fragment visibility goes missing)
Cost
Measured against #3310 on the resolution-time workloads in Node, WGSL identical
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Follow-up to #3310 and #3220, shape proposed by @iwoplaza. Draft implementation: #3326
Problem
Since #3310, automatic bindings (
root.createUniform(...)etc. used directly in a shader) are only visible to the stages that use them:To get there, resolution has to remember which stage each binding was used from. The catch is memoization:
wobblereads none, so it's resolved oncevertexresolveswobble(miss),timegets recorded withvertexfragmentresolveswobble(hit), the cached snippet comes back and nothing gets recordedtimeends up visible only tovertex, and the fragment shader fails validationAnything an item records into the context (not into the WGSL) has this problem. #3310 solves it for bindings specifically:
usedFixedBindingson every item layer ofItemStateStack_getOrInstantiatestages/usedOutsideStagesonFixedBindingConfig#3220 moves those layers into
SlotScope, which is shared with the CPU path, so none of this has a home there. The next thing that needs to survive caching would need its own copyProbes
Every resolved item gets a set of payloads per probe, cached next to its snippet. Whoever resolves the item merges those sets into its own, on a hit and on a miss
Types (
core/resolve/probe.ts)Context (
ResolutionCtx)Rules
reportadds to the top one{ slotToValueMap, result, probes }ctx.resolve(item)returns, hit or miss, the item's payloads are re-reported into the caller's frame, so they pass the caller's mapsresolve()reads it once everything is resolvedProperties
Set), producers that combine values intern themmemoizedLazy) don't get frames, nothing can report while a lazy computes (bindings are allocated when a buffer use is resolved)'use gpu'functions resolved directly aren't memoized and get no frame, the shellless instance they resolve to has oneResolutionCtx(exported fromtypegpu/~internal) gainsreport,withProbeMapandwithShaderStage#3310 on probes
Reporting (
core/resolve/bindingUse.ts)Stage boundary
withShaderStagereplacesctx.withSlots([[shaderStageSlot, stage]], ...)at the 5 entry sites (tgpuComputeFn,tgpuVertexFn,tgpuFragmentFn,autoIOx2):Reading (
fixedLayoutEntries, called fromresolve())stage: undefined(used outside an entry) → keep the layout entry as is, visible everywhere like beforevisibilityfrom the used stages, inALL_STAGESorder (compute,vertex,fragment)The example again
What goes away
usedFixedBindingson item layersuseFixedBindingwithProbeMapat the entry boundarystages/usedOutsideStagesonFixedBindingConfigresolve()readSlot(slot, trackUsage)Fit with #3220
ResolutionCtxImpl, pushed inside thecomputecallback ofslotScope.getOrInstantiateInstanceMap<TKey, T>already allows thatSlotScopestays untouched, whichever lands second only reconciles the cache entry shapeTests
#3310's binding visibility tests stay as they are and pass unchanged, plus one for plain functions resolved by an entry. Dropping the merge on the hit path fails the vertex + fragment test (fragment visibility goes missing)
Cost
Measured against #3310 on the
resolution-timeworkloads in Node, WGSL identicalSo ~2 µs per binding warm and ~1 ms on the first resolution
All reactions