Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 4 additions & 10 deletions packages/typegpu/src/core/resolve/externals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isLooseData } from '../../data/dataTypes.ts';
import { isWgslStruct } from '../../data/wgslTypes.ts';
import { getName, hasTinyestMetadata, setName } from '../../shared/meta.ts';
import { getName, hasTinyestMetadata, isNamable, setName } from '../../shared/meta.ts';
import { isWgsl, type ResolutionCtx } from '../../types.ts';

/**
Expand Down Expand Up @@ -42,15 +42,6 @@ export function mergeExternals(existing: ExternalMap, newExternals: ExternalMap)
} else {
existing[key] = value;
}

// Giving name to external value, if it does not already have one.
if (
value &&
(typeof value === 'object' || typeof value === 'function') &&
getName(value) === undefined
) {
setName(value, key);
}
}
}

Expand Down Expand Up @@ -115,6 +106,9 @@ export function replaceExternalsInWgsl(
}

if (isResolvable(external)) {
if (isNamable(external) && getName(external) === undefined) {
setName(external, externalName.split('.').at(-1) as string);
}
return acc.replaceAll(externalRegex, ctx.resolve(external).value);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/typegpu/src/core/root/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ import { vec3f, vec3u } from '../../data/vector.ts';
import { u32 } from '../../data/numeric.ts';
import { ceil } from '../../std/numeric.ts';
import { allEq } from '../../std/boolean.ts';
import { setName } from '../../shared/meta.ts';
import { getName, setName } from '../../shared/meta.ts';

/**
* Changes the given array to a vec of 3 numbers, filling missing values with 1.
Expand Down Expand Up @@ -259,6 +259,9 @@ class WithBindingImpl implements WithBinding {

const workgroupSize = workgroupSizeConfigs[callback.length] as v3u;
const wrappedCallback = fn([u32, u32, u32])(callback as (...args: number[]) => void);
if (getName(wrappedCallback) === undefined) {
wrappedCallback.$name('wrappedCallback');
}

const sizeUniform = root.createUniform(vec3u);

Expand Down
5 changes: 4 additions & 1 deletion packages/typegpu/src/resolutionCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import type {
} from './types.ts';
import { CodegenState, isSelfResolvable, NormalState } from './types.ts';
import type { WgslEnableExtension } from './wgslExtensions.ts';
import { getName, hasTinyestMetadata, setName } from './shared/meta.ts';
import { getName, hasTinyestMetadata, isNamable, setName } from './shared/meta.ts';
import { FuncParameterType } from 'tinyest';
import { accessProp } from './tgsl/accessProp.ts';
import { createIoSchema } from './core/function/ioSchema.ts';
Expand Down Expand Up @@ -197,6 +197,9 @@ class ItemStateStackImpl implements ItemStateStack {
}

const external = layer.externalMap[id];
if (isNamable(external) && getName(external) === undefined) {
setName(external, id);
}

if (external !== undefined && external !== null) {
return coerceToSnippet(external);
Expand Down
7 changes: 6 additions & 1 deletion packages/typegpu/src/tgsl/accessProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { isKnownAtComptime } from '../types.ts';
import { coerceToSnippet, numericLiteralToSnippet } from './generationHelpers.ts';
import { InfixDispatch, infixOperators, type InfixOperatorName } from './infixDispatch.ts';
import { accessStructProp } from './accessStructProp.ts';
import { getName, isNamable, setName } from '../shared/meta.ts';

const infixKinds = [
'vec2f',
Expand Down Expand Up @@ -205,7 +206,11 @@ export function accessProp(target: Snippet, propName: string): Snippet | undefin

if (isKnownAtComptime(target) || target.dataType === UnknownData) {
// oxlint-disable-next-line typescript/no-explicit-any -- we either know exactly what it is, or have no idea at all
return coerceToSnippet((target.value as any)[propName]);
const prop = (target.value as any)[propName];
if (isNamable(prop) && getName(prop) === undefined) {
setName(prop, propName);
}
return coerceToSnippet(prop);
}

return undefined;
Expand Down
36 changes: 36 additions & 0 deletions packages/typegpu/tests/rawFn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,42 @@ describe('tgpu.fn with raw wgsl and missing types', () => {
}"
`);
});

it('names resolved externals', () => {
const c1 = (() => tgpu.const(d.vec2u, d.vec2u(1)))(); // unnamed
const c2 = (() => tgpu.const(d.vec2u, d.vec2u(2)))(); // unnamed
const c3 = (() => tgpu.const(d.vec2u, d.vec2u(3)))(); // unnamed
const fn = tgpu.fn([])`() {
let a = n1;
let b = ext.n2;
let c = ext.n3.x;
}`.$uses({
n1: c1,
ext: { n2: c2, n3: c3 },
});

expect(tgpu.resolve([fn])).toMatchInlineSnapshot(`
"const n1: vec2u = vec2u(1);

const n2: vec2u = vec2u(2);

const n3: vec2u = vec2u(3);

fn fn_1() {
let a = n1;
let b = n2;
let c = n3.x;
}"
`);
});

it("resolved externals's names stay the same", () => {
const c1 = (() => tgpu.const(d.vec2u, d.vec2u(1)))(); // unnamed
tgpu.resolve([tgpu.fn([])`() { let a = myConst; }`.$uses({ myConst: c1 })]);
tgpu.resolve([tgpu.fn([])`() { let a = otherName; }`.$uses({ otherName: c1 })]);

expect(getName(c1)).toMatchInlineSnapshot(`"myConst"`);
});
Comment on lines +571 to +577

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This makes me wonder if we should do anything at all, though I don't think it's that much of an issue

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it's fine, I think it's unusual for a thing to be references by other names in different WGSL templates, and even if it is, it's still a valid name for it.

});

describe('tgpu.computeFn with raw string WGSL implementation', () => {
Expand Down
32 changes: 32 additions & 0 deletions packages/typegpu/tests/tgslFn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,4 +1077,36 @@ describe('tgsl fn when using plugin', () => {
}"
`);
});

it('names used externals', () => {
const myConst = (() => tgpu.const(d.u32, 1))(); // unnamed
const fn = () => {
'use gpu';
return myConst.$;
};

expect(tgpu.resolve([fn])).toMatchInlineSnapshot(`
"const myConst: u32 = 1u;

fn fn_1() -> u32 {
return myConst;
}"
`);
});

it('names used nested externals', () => {
const EXT = { myConst: (() => tgpu.const(d.u32, 1))() /* unnamed */ };
const fn = () => {
'use gpu';
return EXT.myConst.$;
};

expect(tgpu.resolve([fn])).toMatchInlineSnapshot(`
"const myConst: u32 = 1u;

fn fn_1() -> u32 {
return myConst;
}"
`);
});
});
Loading