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
Being able to represent WGSL-specific constructs in JS.
JS running as you would expect, both on the CPU and CPU.
There are a couple of hurdles we need to overcome:
WGSL has explicit control over if a parameter gets copied or passed by reference (ptr<>), while in JS, passing primitives always copies, and passing non-primitives does so by reference.
Assignments copy by default in WGSL
Numbers have types more precise than just number (i32, u32, f32), yet we cannot overload operators in JS
Value/Reference semantics
Passing primitives by value & passing non-primitives by reference
Not much to do here, primitives are always copied in JS and non-primitives are passed by reference, so we can just fall-through to default JS behavior
main() would return 1, since increment is able to act directly on count
How could the equivalent JS code look like? For increment to have direct access to count, the value needs to be non-primitive. To achieve that, we can let developers create boxed values (essentially wrap them in an object with one property). This is only necessary for primitive values, as non-primitives are passed by reference automatically.
constincrement=tgpu.fn([d.ptrFn(d.f32)])((val)=>{// `val` is of type `d.boxed<number>`, `val.$` is of type `number`val.$+=1;});constmain=tgpu.fn([],d.f32)(()=>{constcount=d.box(0);// => d.boxed<number>increment(count);returncount.$;});
Passing non-primitives by value
We can just copy the passed arguments when a function is called, which is what we already do
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 is a draft discussion, expect updates on it
We have 2 goals with TGSL:
There are a couple of hurdles we need to overcome:
ptr<>), while in JS, passing primitives always copies, and passing non-primitives does so by reference.number(i32, u32, f32), yet we cannot overload operators in JSValue/Reference semantics
Passing primitives by value & passing non-primitives by reference
Not much to do here, primitives are always copied in JS and non-primitives are passed by reference, so we can just fall-through to default JS behavior
Passing primitives by reference
Let's ponder about the following WGSL code
main()would return 1, sinceincrementis able to act directly oncountHow could the equivalent JS code look like? For
incrementto have direct access tocount, the value needs to be non-primitive. To achieve that, we can let developers createboxedvalues (essentially wrap them in an object with one property). This is only necessary for primitive values, as non-primitives are passed by reference automatically.Passing non-primitives by value
We can just copy the passed arguments when a function is called, which is what we already do
Beta Was this translation helpful? Give feedback.
All reactions