Skip to content

feat: Automatically assign names to resources in Unplugin #1344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

aleksanderkatan
Copy link
Contributor

@aleksanderkatan aleksanderkatan commented Jun 9, 2025

The high-level rule for autonaming is like this: if there is an assignment to a variable, we try to find one of the resourceConstructors among the identifiers in the assigned expression; if we succeed (and we don't find the $name identifier), we wrap the expression in a call to __TYPEGPU_AUTONAME__. If typegpu is initialized, this function checks if the expression has the $internal symbol, and names it (using $name) if it isn't already named.

I also needed to add [$internal]: true to some of the resources.

Let me know if I missed any of the resource generating functions.

Copy link

github-actions bot commented Jun 10, 2025

pkg.pr.new

packages

pnpm i https://pkg.pr.new/software-mansion/TypeGPU/typegpu@1344
pnpm i https://pkg.pr.new/software-mansion/TypeGPU/typegpu@24acc0cf048c67ae4ae1a735489952f93186ff51

benchmark
view benchmark

commit
view commit

@aleksanderkatan aleksanderkatan marked this pull request as ready for review June 11, 2025 15:05
@aleksanderkatan
Copy link
Contributor Author

aleksanderkatan commented Jun 11, 2025

At least +500, -100 of these changes are tests :)

<moved to description>

@aleksanderkatan
Copy link
Contributor Author

aleksanderkatan commented Jun 11, 2025

As of right now, functions marked with our directives are not assigned any names (no $name method, we need to save the names in the metadata). I decided to delegate that to another ticket: #1358

@reczkok
Copy link
Contributor

reczkok commented Jun 11, 2025

At least +500, -100 of these changes are tests :)

The high-level rule for autonaming is like this: if there is an assignment to a variable, we try to find one of the resourceConstructors among the identifiers in the assigned expression; if we succeed (and we don't find the $name identifier), we wrap the expression in a call to __TYPEGPU_AUTONAME__. If typegpu is initialized, this function checks if the expression has the $internal symbol, and names it (using $name) if it isn't already named.

I also needed to add [$internal]: true to some of the resources.

Let me know if I missed any of the resource generating functions.

Please add that to the PR description

Copy link
Contributor

@mhawryluk mhawryluk left a comment

Choose a reason for hiding this comment

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

Great job!! 🦾

@aleksanderkatan
Copy link
Contributor Author

I added fn, vertexFn etc. to resourceConstructors. Now as a side effect shell creations are also wrapped in the autoname, but the reason for including this is so that TaggedTemplateExpressions, which don't fall under the shell calls category, are autonamed.

const myFn = tgpu['~unstable'].fn([Item], Item) /* wgsl */`(item: Item) -> Item { return item; }`;

Copy link
Collaborator

@iwoplaza iwoplaza left a comment

Choose a reason for hiding this comment

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

Amazing work, a great push forward! 👏👏👏

'(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (',
).appendRight(
node.end,
`) , ${metadata}) && $.f)({}))`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this space here? 👀

Suggested change
`) , ${metadata}) && $.f)({}))`,
`), ${metadata}) && $.f)({}))`,

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.

feat(unplugin-typegpu): Automatically assign names to functions, slots, etc...
4 participants