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

Merged
merged 29 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cdfff3a
[ROLLUP] extract `assignMetadata` function
May 28, 2025
7c7caa0
[ROLLUP] Add auto naming tests
May 29, 2025
0def8d7
[ROLLUP] auto-name everything
May 29, 2025
eb49600
Add symbol to globalThis
Jun 9, 2025
58da68e
Merge remote-tracking branch 'origin/main' into feat/unplugin-typegpu…
Jun 9, 2025
ee38711
Move the autoname function to globalThis
Jun 10, 2025
fb46bbc
Add filtering for what to autoname
Jun 10, 2025
ecdb1db
Update tests
Jun 10, 2025
8a500c0
Lint
Jun 10, 2025
c71d41e
Add autoname tests
Jun 10, 2025
c6421d2
Add option to disable autonaming
Jun 11, 2025
7e91cde
Add check for `[$internal]`, add `[$internal]: true` to some resources
Jun 11, 2025
05e7ba3
Update parentheses
Jun 11, 2025
820f6d8
Update tests
Jun 11, 2025
6ab2c19
Optimize when `$name` prop is found
Jun 11, 2025
ed1c852
Update babel plugin
Jun 11, 2025
ea7ff21
Lint
Jun 11, 2025
02b62c6
Add more constructors to the list and tests
Jun 11, 2025
694d108
Add `[$internal]:true` to entry functions
Jun 11, 2025
b049689
Fix tests
Jun 11, 2025
d2d0b77
Expose fixed sampler api, autoname samplers
Jun 12, 2025
e981117
Disable auto-naming by default in tests
Jun 12, 2025
d1258e6
Add tests for nested calls, add tagged function handling
Jun 12, 2025
c5fac21
Merge branch 'main' into feat/unplugin-typegpu-automatically-assign-n…
aleksanderkatan Jun 13, 2025
d220450
Merge branch 'main' into feat/unplugin-typegpu-automatically-assign-n…
aleksanderkatan Jun 16, 2025
d1467ed
Merge remote-tracking branch 'origin/main' into feat/unplugin-typegpu…
Jun 23, 2025
24acc0c
Merge fixes
Jun 23, 2025
b4ff3e2
Remove extra space
Jun 24, 2025
d0a28b2
Merge branch 'main' into feat/unplugin-typegpu-automatically-assign-n…
aleksanderkatan Jun 24, 2025
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
3 changes: 3 additions & 0 deletions packages/typegpu/src/core/buffer/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { StorageFlag } from '../../extension.ts';
import type { TgpuNamable } from '../../shared/meta.ts';
import { getName, setName } from '../../shared/meta.ts';
import type { Infer, InferPartial, MemIdentity } from '../../shared/repr.ts';
import { $internal } from '../../shared/symbols.ts';
import type { UnionToIntersection } from '../../shared/utilityTypes.ts';
import { isGPUBuffer } from '../../types.ts';
import type { ExperimentalTgpuRoot } from '../root/rootTypes.ts';
Expand Down Expand Up @@ -72,6 +73,7 @@ const usageToUsageConstructor = {
};

export interface TgpuBuffer<TData extends BaseData> extends TgpuNamable {
readonly [$internal]: true;
readonly resourceType: 'buffer';
readonly dataType: TData;
readonly initial?: Infer<TData> | undefined;
Expand Down Expand Up @@ -135,6 +137,7 @@ type RestrictVertexUsages<TData extends BaseData> = TData extends {
: 'vertex'[];

class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {
public readonly [$internal] = true;
public readonly resourceType = 'buffer';
public flags: GPUBufferUsageFlags = GPUBufferUsage.COPY_DST |
GPUBufferUsage.COPY_SRC;
Expand Down
5 changes: 4 additions & 1 deletion packages/typegpu/src/core/function/tgpuComputeFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
setName,
type TgpuNamable,
} from '../../shared/meta.ts';
import { $getNameForward } from '../../shared/symbols.ts';
import { $getNameForward, $internal } from '../../shared/symbols.ts';
import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
import { createFnCore, type FnCore } from './fnCore.ts';
import type { Implementation, InferIO, IORecord } from './fnTypes.ts';
Expand Down Expand Up @@ -74,6 +74,7 @@ export interface TgpuComputeFn<
// biome-ignore lint/suspicious/noExplicitAny: to allow assigning any compute fn to TgpuComputeFn (non-generic) type
ComputeIn extends IORecord<AnyComputeBuiltin> = any,
> extends TgpuNamable {
readonly [$internal]: true;
readonly shell: TgpuComputeFnShellHeader<ComputeIn>;

$uses(dependencyMap: Record<string, unknown>): this;
Expand Down Expand Up @@ -148,6 +149,7 @@ function createComputeFn<ComputeIn extends IORecord<AnyComputeBuiltin>>(
implementation: Implementation,
): TgpuComputeFn<ComputeIn> {
type This = TgpuComputeFn<ComputeIn> & SelfResolvable & {
[$internal]: true;
[$getNameForward]: FnCore;
};

Expand All @@ -162,6 +164,7 @@ function createComputeFn<ComputeIn extends IORecord<AnyComputeBuiltin>>(
return this;
},

[$internal]: true,
[$getNameForward]: core,
$name(newLabel: string): This {
setName(core, newLabel);
Expand Down
9 changes: 7 additions & 2 deletions packages/typegpu/src/core/function/tgpuFragmentFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
setName,
type TgpuNamable,
} from '../../shared/meta.ts';
import { $getNameForward } from '../../shared/symbols.ts';
import { $getNameForward, $internal } from '../../shared/symbols.ts';
import type { GenerationCtx } from '../../tgsl/generationHelpers.ts';
import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
import { addReturnTypeToExternals } from '../resolve/externals.ts';
Expand Down Expand Up @@ -108,6 +108,7 @@ export interface TgpuFragmentFn<
Varying extends FragmentInConstrained = FragmentInConstrained,
Output extends FragmentOutConstrained = FragmentOutConstrained,
> extends TgpuNamable {
readonly [$internal]: true;
readonly shell: TgpuFragmentFnShellHeader<Varying, Output>;
readonly outputType: IOLayoutToSchema<Output>;

Expand Down Expand Up @@ -179,7 +180,10 @@ function createFragmentFn(
>,
implementation: Implementation,
): TgpuFragmentFn {
type This = TgpuFragmentFn & SelfResolvable & { [$getNameForward]: FnCore };
type This = TgpuFragmentFn & SelfResolvable & {
[$internal]: true;
[$getNameForward]: FnCore;
};

const core = createFnCore(shell, implementation);
const outputType = shell.returnType;
Expand All @@ -201,6 +205,7 @@ function createFragmentFn(
return this;
},

[$internal]: true,
[$getNameForward]: core,
$name(newLabel: string): This {
setName(core, newLabel);
Expand Down
5 changes: 4 additions & 1 deletion packages/typegpu/src/core/function/tgpuVertexFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
setName,
type TgpuNamable,
} from '../../shared/meta.ts';
import { $getNameForward } from '../../shared/symbols.ts';
import { $getNameForward, $internal } from '../../shared/symbols.ts';
import type { GenerationCtx } from '../../tgsl/generationHelpers.ts';
import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
import { addReturnTypeToExternals } from '../resolve/externals.ts';
Expand Down Expand Up @@ -90,6 +90,7 @@ export interface TgpuVertexFn<
// biome-ignore lint/suspicious/noExplicitAny: to allow assigning any vertex fn to TgpuVertexFn (non-generic) type
VertexOut extends VertexOutConstrained = any,
> extends TgpuNamable {
readonly [$internal]: true;
readonly shell: TgpuVertexFnShellHeader<VertexIn, VertexOut>;
readonly outputType: IOLayoutToSchema<VertexOut>;
readonly inputType: IOLayoutToSchema<VertexIn> | undefined;
Expand Down Expand Up @@ -167,6 +168,7 @@ function createVertexFn(
& TgpuVertexFn<VertexInConstrained, VertexOutConstrained>
& SelfResolvable
& {
[$internal]: true;
[$getNameForward]: FnCore;
};

Expand All @@ -191,6 +193,7 @@ function createVertexFn(
return this;
},

[$internal]: true,
[$getNameForward]: core,
$name(newLabel: string): This {
setName(core, newLabel);
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/src/core/sampler/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ export interface TgpuFixedSampler extends TgpuSampler, TgpuNamable {}
export interface TgpuFixedComparisonSampler
extends TgpuComparisonSampler, TgpuNamable {}

export function sampler(props: SamplerProps): TgpuSampler {
export function sampler(props: SamplerProps): TgpuFixedSampler {
return new TgpuFixedSamplerImpl(props);
}

export function comparisonSampler(
props: ComparisonSamplerProps,
): TgpuComparisonSampler {
): TgpuFixedComparisonSampler {
return new TgpuFixedComparisonSamplerImpl(props);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/typegpu/src/core/slot/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
$getNameForward,
$gpuRepr,
$gpuValueOf,
$internal,
$repr,
$wgslDataType,
} from '../../shared/symbols.ts';
Expand Down Expand Up @@ -37,6 +38,7 @@ export function accessor<T extends AnyWgslData>(

export class TgpuAccessorImpl<T extends AnyWgslData>
implements TgpuAccessor<T>, SelfResolvable {
public readonly [$internal] = true;
public readonly resourceType = 'accessor';
public readonly slot: TgpuSlot<
TgpuFn<() => T> | TgpuBufferUsage<T> | Infer<T>
Expand Down
8 changes: 7 additions & 1 deletion packages/typegpu/src/core/slot/slot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { getResolutionCtx } from '../../gpuMode.ts';
import { getName, setName } from '../../shared/meta.ts';
import type { Infer, InferGPU } from '../../shared/repr.ts';
import { $gpuRepr, $gpuValueOf, $repr } from '../../shared/symbols.ts';
import {
$gpuRepr,
$gpuValueOf,
$internal,
$repr,
} from '../../shared/symbols.ts';
import type { ResolutionCtx } from '../../types.ts';
import { getGpuValueRecursively } from '../valueProxyUtils.ts';
import type { TgpuSlot } from './slotTypes.ts';
Expand All @@ -19,6 +24,7 @@ export function slot<T>(defaultValue?: T): TgpuSlot<T> {
// --------------

class TgpuSlotImpl<T> implements TgpuSlot<T> {
public readonly [$internal] = true;
public readonly resourceType = 'slot';

// Type-tokens, not available at runtime
Expand Down
9 changes: 8 additions & 1 deletion packages/typegpu/src/core/slot/slotTypes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import type { AnyData } from '../../data/dataTypes.ts';
import type { TgpuNamable } from '../../shared/meta.ts';
import type { Infer, InferGPU } from '../../shared/repr.ts';
import { $gpuRepr, $providing, $repr } from '../../shared/symbols.ts';
import {
$gpuRepr,
$internal,
$providing,
$repr,
} from '../../shared/symbols.ts';
import type { TgpuFn } from '../function/tgpuFn.ts';
import type { TgpuBufferUsage } from './../buffer/bufferUsage.ts';

export interface TgpuSlot<T> extends TgpuNamable {
readonly [$internal]: true;
readonly resourceType: 'slot';

// Type-tokens, not available at runtime
Expand Down Expand Up @@ -43,6 +49,7 @@ export interface TgpuDerived<T> {
}

export interface TgpuAccessor<T extends AnyData = AnyData> extends TgpuNamable {
readonly [$internal]: true;
readonly resourceType: 'accessor';

// Type-tokens, not available at runtime
Expand Down
3 changes: 3 additions & 0 deletions packages/typegpu/src/core/vertexLayout/vertexLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isDecorated, isWgslStruct } from '../../data/wgslTypes.ts';
import { roundUp } from '../../mathUtils.ts';
import type { TgpuNamable } from '../../shared/meta.ts';
import { setName } from '../../shared/meta.ts';
import { $internal } from '../../shared/symbols.ts';
import {
kindToDefaultFormatMap,
type TgpuVertexAttrib,
Expand All @@ -29,6 +30,7 @@ import type {
export interface TgpuVertexLayout<
TData extends WgslArray | Disarray = WgslArray | Disarray,
> extends TgpuNamable {
readonly [$internal]: true;
readonly resourceType: 'vertex-layout';
readonly stride: number;
readonly stepMode: 'vertex' | 'instance';
Expand Down Expand Up @@ -157,6 +159,7 @@ function dataToContainedAttribs<

class TgpuVertexLayoutImpl<TData extends WgslArray | Disarray>
implements TgpuVertexLayout<TData> {
public readonly [$internal] = true;
public readonly resourceType = 'vertex-layout';
public readonly stride: number;
public readonly attrib: ArrayToContainedAttribs<TData>;
Expand Down
11 changes: 10 additions & 1 deletion packages/typegpu/src/shared/meta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Block, FuncParameter } from 'tinyest';
import { $getNameForward } from './symbols.ts';
import { $getNameForward, $internal } from './symbols.ts';

export interface MetaData {
name?: string | undefined;
Expand All @@ -14,8 +14,17 @@ export interface MetaData {

interface GlobalWithMeta {
__TYPEGPU_META__: WeakMap<object, MetaData>;
__TYPEGPU_AUTONAME__: <T>(exp: T, label: string) => T;
}

Object.assign(globalThis, {
'__TYPEGPU_AUTONAME__': <T>(exp: T, label: string): T =>
isNamable(exp) &&
(exp as unknown as { [$internal]: unknown })?.[$internal] && !getName(exp)
? exp.$name(label)
: exp,
});

function isForwarded(value: unknown): value is { [$getNameForward]: unknown } {
return !!(value as { [$getNameForward]?: unknown })?.[$getNameForward];
}
Expand Down
3 changes: 3 additions & 0 deletions packages/typegpu/src/tgpuBindGroupLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
import type { TgpuNamable } from './shared/meta.ts';
import { getName, setName } from './shared/meta.ts';
import type { Infer, MemIdentity } from './shared/repr.ts';
import { $internal } from './shared/symbols.ts';
import type { Default, Prettify } from './shared/utilityTypes.ts';
import type { TgpuShaderStage } from './types.ts';
import type { Unwrapper } from './unwrapper.ts';
Expand Down Expand Up @@ -158,6 +159,7 @@ export interface TgpuBindGroupLayout<
TgpuLayoutEntry | null
>,
> extends TgpuNamable {
readonly [$internal]: true;
readonly resourceType: 'bind-group-layout';
readonly entries: Entries;
readonly bound: {
Expand Down Expand Up @@ -391,6 +393,7 @@ const DEFAULT_READONLY_VISIBILITY: TgpuShaderStage[] = [
class TgpuBindGroupLayoutImpl<
Entries extends Record<string, TgpuLayoutEntry | null>,
> implements TgpuBindGroupLayout<Entries> {
public readonly [$internal] = true;
private _index: number | undefined;

public readonly resourceType = 'bind-group-layout' as const;
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/tests/bindGroupLayout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('TgpuBindGroupLayout', () => {
root.unwrap(bindGroup);

expect(root.device.createBindGroup).toBeCalledWith({
label: '<unnamed>',
label: 'layout',
layout: root.unwrap(layout),
entries: [
{
Expand All @@ -167,7 +167,7 @@ describe('TgpuBindGroupLayout', () => {
});

expect(root.device.createBindGroupLayout).toBeCalledWith({
label: '<unnamed>',
label: 'layout',
entries: [
{
binding: 0,
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/tests/buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('TgpuBuffer', () => {

root.unwrap(dataBuffer);
expect(root.device.createBuffer).toBeCalledWith({
label: '<unnamed>',
label: 'dataBuffer',
mappedAtCreation: false,
size: 64,
usage: global.GPUBufferUsage.UNIFORM |
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('TgpuBuffer', () => {
// First call (raw buffer)
[
{
label: '<unnamed>',
label: 'buffer',
mappedAtCreation: false,
size: 12,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
Expand Down
12 changes: 6 additions & 6 deletions packages/typegpu/tests/computePipeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, expectTypeOf, vi } from 'vitest';
import type { TgpuQuerySet } from '../src/core/querySet/querySet.ts';
import * as d from '../src/data/index.ts';
import tgpu, {
MissingBindGroupsError,
Expand All @@ -7,7 +8,6 @@ import tgpu, {
} from '../src/index.ts';
import { $internal } from '../src/shared/symbols.ts';
import { it } from './utils/extendedIt.ts';
import type { TgpuQuerySet } from '../src/core/querySet/querySet.ts';

describe('TgpuComputePipeline', () => {
it('can be created with a compute entry function', ({ root, device }) => {
Expand Down Expand Up @@ -289,7 +289,7 @@ describe('TgpuComputePipeline', () => {
pipeline.dispatchWorkgroups(1);

expect(commandEncoder.beginComputePass).toHaveBeenCalledWith({
label: '<unnamed>',
label: 'pipeline',
timestampWrites: {
querySet: querySet.querySet,
beginningOfPassWriteIndex: 1,
Expand Down Expand Up @@ -342,7 +342,7 @@ describe('TgpuComputePipeline', () => {
pipeline2.dispatchWorkgroups(1);

expect(commandEncoder.beginComputePass).toHaveBeenCalledWith({
label: '<unnamed>',
label: 'pipeline',
timestampWrites: {
querySet: querySet.querySet,
beginningOfPassWriteIndex: 0,
Expand All @@ -351,7 +351,7 @@ describe('TgpuComputePipeline', () => {
});

expect(commandEncoder.beginComputePass).toHaveBeenCalledWith({
label: '<unnamed>',
label: 'pipeline2',
timestampWrites: {
querySet: querySet.querySet,
beginningOfPassWriteIndex: 2,
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('TgpuComputePipeline', () => {
pipeline.dispatchWorkgroups(1);

expect(commandEncoder.beginComputePass).toHaveBeenCalledWith({
label: '<unnamed>',
label: 'pipeline',
timestampWrites: {
querySet: querySet.querySet,
beginningOfPassWriteIndex: 3,
Expand Down Expand Up @@ -438,7 +438,7 @@ describe('TgpuComputePipeline', () => {
pipeline.dispatchWorkgroups(1);

expect(commandEncoder.beginComputePass).toHaveBeenCalledWith({
label: '<unnamed>',
label: 'pipeline',
timestampWrites: {
querySet: querySet.querySet,
beginningOfPassWriteIndex: 2,
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/tests/renderPipeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, expectTypeOf, vi } from 'vitest';
import type { TgpuQuerySet } from '../src/core/querySet/querySet.ts';
import * as d from '../src/data/index.ts';
import tgpu, {
MissingBindGroupsError,
Expand All @@ -8,7 +9,6 @@ import tgpu, {
} from '../src/index.ts';
import { $internal } from '../src/shared/symbols.ts';
import { it } from './utils/extendedIt.ts';
import type { TgpuQuerySet } from '../src/core/querySet/querySet.ts';

describe('Inter-Stage Variables', () => {
describe('Non-empty vertex output', () => {
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('Inter-Stage Variables', () => {

expect(commandEncoder.beginRenderPass).toHaveBeenCalledWith(
expect.objectContaining({
label: '<unnamed>',
label: 'pipeline',
timestampWrites: {
querySet: querySet.querySet,
beginningOfPassWriteIndex: 1,
Expand Down
Loading