Skip to content

Conversation

@iwoplaza
Copy link
Collaborator

@iwoplaza iwoplaza commented Nov 15, 2025

Changes:

  • externals is now an arrow function instead of a getter, so that it can capture the this value from the outer scope.
  • this is treated like any other identifier, except for when generating the metadata object in unplugin. We cannot use shorthand syntax for this
  • tgpu['~unstable'].comptime is a way to create functions that are callable from within shader functions, but only allowed to execute at compile-time. They accept only compile-time known arguments, and throw otherwise.
  • tgpu['~unstable'].rawCodeSnippet lets a JS-implemented function refer to a WGSL expression that TypeGPU doesn't have control over (like in the case of our Three.js integration)

@github-actions
Copy link

github-actions bot commented Nov 20, 2025

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/typegpu@9d9c2fed1d752859e1cbd2b69a5218bf206ff15e
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/noise@9d9c2fed1d752859e1cbd2b69a5218bf206ff15e
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@9d9c2fed1d752859e1cbd2b69a5218bf206ff15e

benchmark
view benchmark

commit
view commit

@iwoplaza iwoplaza changed the title feat: tgpu.comptime, tgpu.rawCodeSnippet and this allowed in TypeGPU feat: tgpu.comptime, tgpu.rawCodeSnippet and this allowed in TypeGPU shader functions Nov 20, 2025
Copy link
Contributor

@aleksanderkatan aleksanderkatan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great changes! 💻🕑

Please add one sentence about each comptime and rawCodeSnippet to the docs (maybe utils.mdx?) so that people know they exist

type: TDataType,
origin: RawCodeSnippetOrigin | undefined = 'runtime',
): TgpuRawCodeSnippet<TDataType> {
return new TgpuRawCodeSnippetImpl(expression, type, 'runtime');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new TgpuRawCodeSnippetImpl(expression, type, 'runtime');
return new TgpuRawCodeSnippetImpl(expression, type, origin);

ast: ${embedJSON({ params, body, externalNames })},
get externals() { return {${externalNames.join(', ')}}; },
externals: () => ({${
externalNames.map((e) => e === 'this' ? '"this": this' : e).join(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests for this?

// Implementation
// --------------

class TgpuRawCodeSnippetImpl<TDataType extends AnyData>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some resolve tests for the raw code snippet ($uses, constant inlining etc.)

Comment on lines +163 to +165
get value(): InferGPU<TDataType> {
return this.$;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still plan to deprecate and remove the value in favor of $? If so, maybe we can skip adding the value getter in new resources

* };
* ```
*/
export function comptime<T extends (...args: never[]) => unknown>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for this...


// get data generated by the plugin
const pluginData = getMetaData(implementation);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smol

Suggested change
// Passing a record happens prior to version 0.9.0
// TODO: Support for this can be removed down the line

Comment on lines +147 to +151
if (ctx.ignoreExternalDepth === 0) {
ctx.externalNames.add('this');
}

return 'this';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what exactly happens here, and whether this is relevant, but this is a reserved word and cannot be used to name variables, struct props, structs or functions

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true! Could be simplified to return just "this"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants