Skip to content

feat: Allow omitting types in wgsl header #1306

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 21 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/typegpu/src/core/function/extractArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ function strip(
};
}

if (code.isAt('(') && !argsStart) {
if (code.isAt('(') && argsStart === undefined) {
argsStart = code.pos;
}

if (argsStart) {
if (argsStart !== undefined) {
strippedCode += code.str[code.pos];
}
code.advanceBy(1); // parsed character
Expand Down
81 changes: 72 additions & 9 deletions packages/typegpu/src/core/function/fnCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type ExternalMap,
replaceExternalsInWgsl,
} from '../resolve/externals.ts';
import { extractArgs } from './extractArgs.ts';
import type { Implementation } from './fnTypes.ts';

export interface TgpuFnShellBase<Args extends unknown[], Return> {
Expand Down Expand Up @@ -81,31 +82,70 @@ export function createFnCore(
const id = ctx.names.makeUnique(getName(this));

if (typeof implementation === 'string') {
const replacedImpl = replaceExternalsInWgsl(
ctx,
externalMap,
implementation,
);

let header = '';
let body = '';

if (shell.isEntry) {
const input = isWgslStruct(shell.argTypes[0]) ? '(in: In)' : '()';
const input = isWgslStruct(shell.argTypes[0])
? `(in: ${ctx.resolve(shell.argTypes[0])})`
: '()';

const attributes = isWgslData(shell.returnType)
? getAttributesString(shell.returnType)
: '';
const output = shell.returnType !== Void
? isWgslStruct(shell.returnType)
? '-> Out'
? `-> ${ctx.resolve(shell.returnType)}`
: `-> ${attributes !== '' ? attributes : '@location(0)'} ${
ctx.resolve(shell.returnType)
}`
: '';

header = `${input} ${output} `;
body = replacedImpl;
} else {
const providedArgs = extractArgs(replacedImpl);

if (providedArgs.args.length !== shell.argTypes.length) {
throw new Error(
`WGSL implementation has ${providedArgs.args.length} arguments, while the shell has ${shell.argTypes.length} arguments.`,
);
}

const input = providedArgs.args.map((argInfo, i) =>
`${argInfo.identifier}: ${
checkAndReturnType(
ctx,
`parameter ${argInfo.identifier}`,
argInfo.type,
shell.argTypes[i],
)
}`
).join(', ');

const output = shell.returnType === Void
? ''
: `-> ${
checkAndReturnType(
ctx,
'return type',
providedArgs.ret?.type,
shell.returnType,
)
}`;

header = `(${input}) ${output}`;

body = replacedImpl.slice(providedArgs.range.end);
}

const replacedImpl = replaceExternalsInWgsl(
ctx,
externalMap,
`${header}${implementation.trim()}`,
);

ctx.addDeclaration(`${fnAttribute}fn ${id}${replacedImpl}`);
ctx.addDeclaration(`${fnAttribute}fn ${id}${header}${body}`);
} else {
// get data generated by the plugin
const pluginData = getMetaData(implementation);
Expand Down Expand Up @@ -176,3 +216,26 @@ export function createFnCore(

return core;
}

function checkAndReturnType(
ctx: ResolutionCtx,
name: string,
wgslType: string | undefined,
jsType: unknown,
) {
const resolvedJsType = ctx.resolve(jsType).replace(/\s/g, '');

if (!wgslType) {
return resolvedJsType;
}

const resolvedWgslType = wgslType.replace(/\s/g, '');

if (resolvedWgslType !== resolvedJsType) {
throw new Error(
`Type mismatch between TGPU shell and WGSL code string: ${name}, JS type "${resolvedJsType}", WGSL type "${resolvedWgslType}".`,
);
}

return wgslType;
}
10 changes: 10 additions & 0 deletions packages/typegpu/tests/extractArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,14 @@ describe('extract args', () => {
expect(ret).toBeUndefined();
expect(range).toStrictEqual({ begin: 21, end: 57 });
});

it('extracts when no arguments, no name, no return type', () => {
const wgslFn = /* wgsl */ '() { return 42; }';

const { args, ret, range } = extractArgs(wgslFn);

expect(args).toStrictEqual([]);
expect(ret).toStrictEqual(undefined);
expect(range).toStrictEqual({ begin: 0, end: 3 });
});
});
Loading