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
Languages like C++, C#, Java and many more have a way to define generic parameters in functions that abstract over types and constant parameters that the function uses. It's similar to a regular function parameter, except that it operates on values that are known at compile-time, not a runtime. The Zig language makes that delineation even thinner, treating generic parameters the same as regular parameters, except prefixed with the comptime keyword.
We already have a way to do comptime specialization in TypeGPU via tgpu.slot and tgpu.derived, but explaining how to use them properly and using them in practice for generic programming can be a bit cumbersome. This was somewhat solved for parameter types with shellless functions that are inherently generic, but being generic over resources used by a function, or comptime parametrization is still only viable via the slot mechanism.
constgeometricSeries=tgpu.comptime((iterations: number)=>{// A function can have stable tinyest metadata// But the externals have to be recreated on every// invocation of `getPixel`. If we cached these// closures based on the externals, then we could// easily dedup the functions returned from here.// // We could also more easily improve use in React.return(a: number,r: number)=>{'use gpu';letresult=0;for(constioftgpu.unroll(iterations)){result+=a*(r**i);}returnresult;};// We can statically unwrap member accesses, unless the property is computed or a `$` (defensively).// We flatten the unwrapped members into a `cacheKey` member, which will be iterated over and compared// with other instances of the AST to dedup.// {// externals: () => ({ tgpu: { unroll: tgpu.unroll }, iterations }),// cacheKey: [tgpu.unroll, iterations],// }// });constfoo=()=>{'use gpu';constalmostTwo=geometricSeries(32)(1,0.5);};
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.
-
Languages like C++, C#, Java and many more have a way to define generic parameters in functions that abstract over types and constant parameters that the function uses. It's similar to a regular function parameter, except that it operates on values that are known at compile-time, not a runtime. The Zig language makes that delineation even thinner, treating generic parameters the same as regular parameters, except prefixed with the
comptimekeyword.We already have a way to do comptime specialization in TypeGPU via
tgpu.slotandtgpu.derived, but explaining how to use them properly and using them in practice for generic programming can be a bit cumbersome. This was somewhat solved for parameter types with shellless functions that are inherently generic, but being generic over resources used by a function, or comptime parametrization is still only viable via the slot mechanism.Beta Was this translation helpful? Give feedback.
All reactions