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
This proposal depends on the "std.isKnownAtComptime" proposal #3027
Calling shader functions generates a call in WGSL, calling a tgpu.comptime functions requires all arguments to be known at comptime, pregenerates the result and injects it into the WGSL. We currently have no way of extracting mixed comptime/runtime behavior out of a shader function in a reusable way. tgpu.macro would be an API to allow for that.
tgpu.macro would accept a 'use gpu' callback that needs to meet a specific requirement:
After all comptime pruning, there needs to be exactly one return statement.
This lets us essentially inline the expression into the callee, and act as if we wrote it there. The arguments passed into the macro need to be side-effect free, as the arguments could be used multiple times inside of the macro (e.g. tgpu.macro((a) => { 'use gpu'; return a * a; }))
Unlocked capabilities
Using macros & std.isKnownAtComptime, we can create functions that precompute if all arguments are known at comptime, and generate a function call otherwise.
/** * Unrolls the iterable if its length is known, falls back to a rolled loop otherwise. */consttryUnroll=tgpu.macro(<Textendsunknown[]>(iterable: T): T=>{'use gpu';returnstd.isKnownAtComptime(iterable.length) ? tgpu.unroll(iterable) : iterable;});functionmain(){'use gpu';for(constboidoftryUnroll(boids.$)){// ...}}
withTryComptime utility
Using macros, we can auto-magically run a function at comp-time if the argument is known at comptime, and at runtime otherwise
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.
Note
This proposal depends on the "std.isKnownAtComptime" proposal #3027
Calling shader functions generates a call in WGSL, calling a tgpu.comptime functions requires all arguments to be known at comptime, pregenerates the result and injects it into the WGSL. We currently have no way of extracting mixed comptime/runtime behavior out of a shader function in a reusable way.
tgpu.macrowould be an API to allow for that.tgpu.macrowould accept a'use gpu'callback that needs to meet a specific requirement:This lets us essentially inline the expression into the callee, and act as if we wrote it there. The arguments passed into the macro need to be side-effect free, as the arguments could be used multiple times inside of the macro (e.g.
tgpu.macro((a) => { 'use gpu'; return a * a; }))Unlocked capabilities
std.isKnownAtComptime, we can create functions that precompute if all arguments are known at comptime, and generate a function call otherwise.Overly simplified example
tryUnrollutilitywithTryComptimeutilityUsing macros, we can auto-magically run a function at comp-time if the argument is known at comptime, and at runtime otherwise
When we support the
...argsrest parameter, we can turn this into a general utility:All reactions