Skip to content

feat: Allow destructuring arguments in TGSL function implementations #1257

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

Merged
merged 16 commits into from
May 27, 2025

Conversation

mhawryluk
Copy link
Contributor

@mhawryluk mhawryluk commented May 16, 2025

closes #1242
closes #1270
closes #1271

Copy link

github-actions bot commented May 16, 2025

pkg.pr.new

packages

pnpm i https://pkg.pr.new/software-mansion/TypeGPU/typegpu@1257
pnpm i https://pkg.pr.new/software-mansion/TypeGPU/typegpu@15daabb0e918a640452a3fc421c20c14c498534c

benchmark
view benchmark

commit
view commit

Copy link
Contributor

@reczkok reczkok left a comment

Choose a reason for hiding this comment

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

Very nice feature to have! I was a little worried about shadowing but it works well 🙇‍♂️

@mhawryluk mhawryluk marked this pull request as draft May 21, 2025 09:05
@mhawryluk mhawryluk marked this pull request as ready for review May 22, 2025 14:33
@mhawryluk mhawryluk changed the title feat: Allow destructuring arguments in entry function TGSL implementations feat: Allow destructuring arguments in TGSL function implementations May 22, 2025
Copy link
Contributor

@reczkok reczkok left a comment

Choose a reason for hiding this comment

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

Amazing change! 💜

Comment on lines 198 to 201
const argAlias = layer.argAliases[id];
if (argAlias !== undefined) {
return argAlias;
}
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
const argAlias = layer.argAliases[id];
if (argAlias !== undefined) {
return argAlias;
}
if (layer.argAliases[id]) {
return layer.argAliases[id];
}

Comment on lines -123 to +153
applyExternals(
externalMap,
Object.fromEntries(
ast.argNames.props.map(({ prop, alias }) => [alias, prop]),
),
);
}

if (
!Array.isArray(shell.argTypes) &&
ast.argNames.type === 'identifiers' &&
ast.argNames.names[0] !== undefined
) {
applyExternals(externalMap, {
[ast.argNames.names[0]]: Object.fromEntries(
Object.keys(shell.argTypes).map((arg) => [arg, arg]),
),
});
}

// Verifying all required externals are present.
// verify all required externals are present
const missingExternals = ast.externalNames.filter(
(name) => !(name in externalMap),
);

if (missingExternals.length > 0) {
throw new MissingLinksError(getName(this), missingExternals);
}

const args: Snippet[] = Array.isArray(shell.argTypes)
? ast.argNames.type === 'identifiers'
? shell.argTypes.map((arg, i) => ({
value: (ast.argNames.type === 'identifiers'
? ast.argNames.names[i]
: undefined) ?? `arg_${i}`,
dataType: arg as AnyWgslData,
}))
: []
: Object.entries(shell.argTypes).map(([name, dataType]) => ({
value: name,
dataType: dataType as AnyWgslData,
}));

// generate wgsl string
const { head, body } = ctx.fnToWgsl({
args,
args: shell.argTypes.map((arg, i) => ({
value: ast.params[i]?.type === FuncParameterType.identifier
? ast.params[i].name
: `_arg_${i}`,
dataType: arg as AnyWgslData,
})),
argAliases: Object.fromEntries(
ast.params.flatMap((param, i) =>
param.type === FuncParameterType.destructuredObject
? param.props.map(({ name, alias }) => [
alias,
{
value: `_arg_${i}.${name}`,
dataType: (shell.argTypes[i] as AnyWgslStruct)
.propTypes[name] as AnyWgslData,
},
])
: []
),
),
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 gonna hang this diff on my wall😍

Comment on lines 457 to 459
params: functionNode.params.filter((param) =>
param.type === 'ObjectPattern' || param.type === 'Identifier'
).map((param) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of filtering here we could check if there are any other and have a nice early error

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!!

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.

Wooh, this is really nice! A pleasure to read 👏

@mhawryluk mhawryluk merged commit 68fd55c into main May 27, 2025
6 checks passed
@mhawryluk mhawryluk deleted the feat/destruct-entry-args branch May 27, 2025 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants