Replies: 1 comment
-
|
There is also another workaround, it is to use classes instead of pojos. import tgpu from 'typegpu';
class Tracker {
count = 0;
tick = tgpu.comptime(() => {
this.count++;
return this.count;
});
}
const tracker = new Tracker();
function main() {
'use gpu';
const value = tracker.tick();
return value + tracker.tick();
}
console.log(main()); // 3
console.log(tgpu.resolve([main])); // 3 + 4I think it would be fine to not handle dynamic this. We could mention it in the docs, and maybe also lint this (POJO && tgpu.comptime && this). |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Let's consider the following code:
This code fails, as
tickis not marked to run at comptime. There's nothing stopping TypeGPU to executetickon a technical level, but for the sake of clarity, we require users to explicitly decide whether they want a function to be executable on the GPU ('use gpu'), or at comptime (tgpu.comptime(...)).To fix this, we can attempt to wrap the function in
tgpu.comptime, and explicitly tell it thatthiswill be bound toTracker.This however fails, both at runtime and during shader generation:
tgpu.comptimecalls the target function directly, without binding the object it's called on. Thereforethisis undefined in the body oftick.thisto anything, so it also leads to the same error.There are workaround to this, things we could do to support this way of writing code, but it greatly complicates the code that has to be emitted by
unplugin-typegpu, for little to no gain.If we always assume that
thisis undefined insidefunction () {}functions, then we can be free to expand property access in externals without handling this particular edge-case.Workaround
If you really want to put functions on objects and use them in shader functions, then don't rely on
this.Beta Was this translation helpful? Give feedback.
All reactions