Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/typegpu/src/core/sampler/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface TgpuSampler {
readonly [$gpuValueOf]: Infer<WgslSampler>;
value: Infer<WgslSampler>;
$: Infer<WgslSampler>;

toString(): string;
}

export interface TgpuComparisonSampler {
Expand All @@ -42,6 +44,8 @@ export interface TgpuComparisonSampler {
readonly [$gpuValueOf]: Infer<WgslComparisonSampler>;
value: Infer<WgslComparisonSampler>;
$: Infer<WgslComparisonSampler>;

toString(): string;
}

export interface TgpuFixedSampler extends TgpuSampler, TgpuNamable {}
Expand Down
2 changes: 2 additions & 0 deletions packages/typegpu/src/core/slot/slotTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface TgpuAccessor<T extends BaseData = BaseData> extends TgpuNamable
*/
readonly value: InferGPU<T>;
readonly $: InferGPU<T>;

toString(): string;
}

type DataAccessorIn<T extends BaseData> =
Expand Down
2 changes: 2 additions & 0 deletions packages/typegpu/src/core/texture/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export interface TgpuTextureView<
readonly [$gpuValueOf]: Infer<TSchema>;
value: Infer<TSchema>;
$: Infer<TSchema>;

toString(): string;
}

export interface TgpuTextureRenderView {
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/accessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, expectTypeOf } from 'vitest';
import tgpu, { d, std, type TgpuAccessor } from '../src/index.js';
import tgpu, { d, std, type TgpuAccessor } from 'typegpu';
import { it } from 'typegpu-testing-utility';

const RED = d.vec3f(1, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/align.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, expectTypeOf, it } from 'vitest';
import { d, tgpu } from '../src/index.js';
import { d, tgpu } from 'typegpu';

describe('d.align', () => {
it('adds @align attribute for custom aligned struct members', () => {
Expand Down
14 changes: 6 additions & 8 deletions packages/typegpu/tests/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { attest } from '@ark/attest';
import { describe, expect, expectTypeOf } from 'vitest';
import { d, tgpu, readFromArrayBuffer, writeToArrayBuffer } from 'typegpu';
import { namespace } from '../src/core/resolve/namespace.ts';
import { resolve } from '../src/resolutionCtx.ts';
import type { Infer } from '../src/shared/repr.ts';
import { arrayLength } from '../src/std/array.ts';
import { d, readFromArrayBuffer, tgpu, writeToArrayBuffer } from 'typegpu';
import type { Infer } from 'typegpu/data';
import { arrayLength } from 'typegpu/std';
import { it } from 'typegpu-testing-utility';

describe('array', () => {
Expand Down Expand Up @@ -67,9 +65,9 @@ describe('array', () => {

expect(() => readFromArrayBuffer(new ArrayBuffer(0), TestArray)).toThrow();

const opts = { namespace: namespace({ names: 'strict' }) };

expect(resolve(TestArray, opts).code).toContain('array<vec3f>');
expect(tgpu.resolve({ template: 'TestArray', externals: { TestArray } })).toEqual(
'array<vec3f>',
);
});

it('throws when trying to nest runtime sized arrays', () => {
Expand Down
17 changes: 2 additions & 15 deletions packages/typegpu/tests/arrayBufferIO.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { attest } from '@ark/attest';
import { describe, expect, expectTypeOf, vi } from 'vitest';
import * as common from '../src/common/index.ts';
import * as d from '../src/data/index.ts';
import { sizeOf } from '../src/data/sizeOf.ts';
import {
patchArrayBuffer,
readFromArrayBuffer,
writeToArrayBuffer,
type ValidateBufferSchema,
type ValidUsagesFor,
} from '../src/index.js';
import { getName } from '../src/shared/meta.ts';
import type { InferPatch, IsValidBufferSchema, IsValidUniformSchema } from '../src/shared/repr.ts';
import type { TypedArray } from '../src/shared/utilityTypes.ts';
import { describe, expect } from 'vitest';
import { d, patchArrayBuffer, readFromArrayBuffer, writeToArrayBuffer } from 'typegpu';
import { it } from 'typegpu-testing-utility';

describe('arrayBufferIO', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/tests/attributes.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, expectTypeOf, it } from 'vitest';
import { d, tgpu } from '../src/index.js';
import { d, tgpu } from 'typegpu';

describe('attributes', () => {
it('adds attributes in the correct order', () => {
Expand Down
47 changes: 14 additions & 33 deletions packages/typegpu/tests/bindGroupLayout.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { beforeEach, describe, expect, expectTypeOf } from 'vitest';
import {
d,
tgpu,
type TgpuBindGroupLayout,
type TgpuBuffer,
type TgpuTextureView,
type UniformFlag,
} from '../src/index.js';
import {
type ExtractBindGroupInputFromLayout,
MissingBindingError,
type TgpuBindGroup,
type TgpuLayoutComparisonSampler,
type TgpuLayoutSampler,
type UnwrapRuntimeConstructor,
} from '../src/tgpuBindGroupLayout.ts';
import { d, tgpu } from 'typegpu';
import type {
TgpuBindGroupLayout,
TgpuBuffer,
TgpuTextureView,
UniformFlag,
ExtractBindGroupInputFromLayout,
TgpuBindGroup,
TgpuLayoutComparisonSampler,
TgpuLayoutSampler,
} from 'typegpu';
import { it } from 'typegpu-testing-utility';

const DEFAULT_READONLY_VISIBILITY_FLAGS =
Expand Down Expand Up @@ -1030,7 +1025,9 @@ describe('TgpuBindGroup', () => {
a: aBuffer,
b: bBuffer,
});
}).toThrow(new MissingBindingError('example', 'd'));
}).toThrowErrorMatchingInlineSnapshot(
`[Error: Bind group 'example' is missing a required binding 'd']`,
);
});

it('creates bind group in layout-defined order, not the insertion order of the populate parameter', ({
Expand Down Expand Up @@ -1131,19 +1128,3 @@ describe('TgpuBindGroup', () => {
});
});
});

describe('UnwrapRuntimeConstructor', () => {
it('unwraps return types of functions returning TgpuData', () => {
expectTypeOf<UnwrapRuntimeConstructor<d.U32>>().toEqualTypeOf<d.U32>();
expectTypeOf<UnwrapRuntimeConstructor<d.WgslArray<d.Vec3f>>>().toEqualTypeOf<
d.WgslArray<d.Vec3f>
>();
expectTypeOf<UnwrapRuntimeConstructor<(_: number) => d.WgslArray<d.Vec3f>>>().toEqualTypeOf<
d.WgslArray<d.Vec3f>
>();

expectTypeOf<UnwrapRuntimeConstructor<d.F32 | ((_: number) => d.U32)>>().toEqualTypeOf<
d.F32 | d.U32
>();
});
});
77 changes: 5 additions & 72 deletions packages/typegpu/tests/buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { attest } from '@ark/attest';
import { describe, expect, expectTypeOf, vi } from 'vitest';
import * as common from '../src/common/index.ts';
import * as d from '../src/data/index.ts';
import { sizeOf } from '../src/data/sizeOf.ts';
import type { ValidateBufferSchema, ValidUsagesFor } from '../src/index.js';
import { getName } from '../src/shared/meta.ts';
import type { InferPatch, IsValidBufferSchema, IsValidUniformSchema } from '../src/shared/repr.ts';
import type { TypedArray } from '../src/shared/utilityTypes.ts';
import * as common from 'typegpu/common';
import * as d from 'typegpu/data';
import { sizeOf } from 'typegpu/data';
import type { ValidateBufferSchema, ValidUsagesFor } from 'typegpu';
import { it } from 'typegpu-testing-utility';

function toUint8Array(...arrays: Array<TypedArray>): Uint8Array {
function toUint8Array(...arrays: Array<ArrayBufferView>): Uint8Array {
let totalByteLength = 0;
for (const arr of arrays) {
totalByteLength += arr.byteLength;
Expand All @@ -31,7 +28,6 @@ describe('TgpuBuffer', () => {

const rawBuffer = root.unwrap(buffer);

expect(getName(buffer)).toBe('myBuffer');
expect(rawBuffer).toBeDefined();
expect(rawBuffer.label).toBe('myBuffer');
});
Expand Down Expand Up @@ -1053,28 +1049,6 @@ describe('TgpuBuffer (.patch() with flexible inputs)', () => {
`);
});

it('should accept tuples, TypedArrays, and number[] for leaf types at the type level', ({
root,
}) => {
const structBuf = root.createBuffer(
d.struct({ pos: d.vec3f, color: d.vec4f, transform: d.mat3x3f }),
);

expectTypeOf<InferPatch<d.Vec3f>>().toEqualTypeOf<
d.v3f | readonly [number, number, number] | Float32Array | undefined
>();

expectTypeOf<InferPatch<d.Mat3x3f>>().toEqualTypeOf<
d.m3x3f | readonly number[] | Float32Array | undefined
>();

// Struct patch should accept flexible types for fields
structBuf.patch({ pos: [1, 2, 3] });
structBuf.patch({ pos: new Float32Array([1, 2, 3]) });
structBuf.patch({ transform: [1, 2, 3, 4, 5, 6, 7, 8, 9] });
structBuf.patch({ transform: new Float32Array(12) });
});

it('should accept both sparse and full-replacement forms for arrays at the type level', ({
root,
}) => {
Expand Down Expand Up @@ -1218,47 +1192,6 @@ describe('TgpuBuffer (.patch() with flexible inputs)', () => {
});
});

describe('IsValidUniformSchema', () => {
it('treats booleans as invalid', () => {
expectTypeOf<IsValidUniformSchema<d.Bool>>().toEqualTypeOf<false>();
});

it('treats numeric schemas as valid', () => {
expectTypeOf<IsValidUniformSchema<d.U32>>().toEqualTypeOf<true>();
});

it('it treats union schemas as valid (even if they contain booleans)', () => {
expectTypeOf<IsValidUniformSchema<d.U32 | d.Bool>>().toEqualTypeOf<true>();
expectTypeOf<IsValidUniformSchema<d.U32 | d.WgslArray<d.Bool>>>().toEqualTypeOf<true>();
expectTypeOf<IsValidUniformSchema<d.WgslArray<d.Bool | d.U32>>>().toEqualTypeOf<true>();
});
});

describe('IsValidBufferSchema', () => {
it('treats booleans as invalid', () => {
expectTypeOf<IsValidBufferSchema<d.Bool>>().toEqualTypeOf<false>();
});

it('treats schemas holding booleans as invalid', () => {
expectTypeOf<IsValidBufferSchema<d.WgslArray<d.Bool>>>().toEqualTypeOf<false>();
expectTypeOf<IsValidBufferSchema<d.WgslStruct<{ a: d.Bool }>>>().toEqualTypeOf<false>();
});

it('treats other schemas as valid', () => {
expectTypeOf<IsValidBufferSchema<d.U32>>().toEqualTypeOf<true>();
});

it('it treats arrays of valid schemas as valid', () => {
expectTypeOf<IsValidBufferSchema<d.WgslArray<d.U32>>>().toEqualTypeOf<true>();
});

it('it treats union schemas as valid (even if they contain booleans)', () => {
expectTypeOf<IsValidBufferSchema<d.U32 | d.Bool>>().toEqualTypeOf<true>();
expectTypeOf<IsValidBufferSchema<d.U32 | d.WgslArray<d.Bool>>>().toEqualTypeOf<true>();
expectTypeOf<IsValidBufferSchema<d.WgslArray<d.Bool | d.U32>>>().toEqualTypeOf<true>();
});
});

describe('ValidateBufferSchema', () => {
it('is strict for exact types', () => {
expectTypeOf<ValidateBufferSchema<d.U32>>().toEqualTypeOf<d.U32>();
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/tests/bufferShorthands.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, expectTypeOf } from 'vitest';
import * as d from '../src/data/index.ts';
import * as d from 'typegpu/data';
import { it } from 'typegpu-testing-utility';
import type {
StorageFlag,
Expand All @@ -8,7 +8,7 @@ import type {
TgpuReadonly,
TgpuUniform,
UniformFlag,
} from '../src/index.js';
} from 'typegpu';
import { attest } from '@ark/attest';

describe('root.createMutable', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/tests/bufferUsage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, expectTypeOf } from 'vitest';

import { d, tgpu } from '../src/index.js';
import type { Infer } from '../src/shared/repr.ts';
import { d, tgpu } from 'typegpu';
import type { Infer } from 'typegpu/data';
import { it } from 'typegpu-testing-utility';

describe('TgpuBufferUniform', () => {
Expand Down
29 changes: 3 additions & 26 deletions packages/typegpu/tests/builtin.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
import { describe, expect, expectTypeOf, it } from 'vitest';
import type { OmitBuiltins } from '../src/builtin.ts';
import * as d from '../src/data/index.ts';
import { namespace } from '../src/core/resolve/namespace.ts';
import { resolve } from '../src/resolutionCtx.ts';
import tgpu from 'typegpu';
import * as d from 'typegpu/data';

describe('builtin', () => {
it('adds a @builtin attribute to a struct field', () => {
const s1 = d.struct({
position: d.builtin.position,
});

const opts = {
namespace: namespace({ names: 'strict' }),
};

expect(resolve(s1, opts).code).toContain('@builtin(position) position: vec4f');
});

it('can be omitted from a record type', () => {
const x = {
a: d.u32,
b: d.builtin.localInvocationId,
c: d.f32,
d: d.builtin.localInvocationIndex,
};

type X = typeof x;
type Omitted = OmitBuiltins<X>;

expectTypeOf<Omitted>().toEqualTypeOf({
a: d.u32,
c: d.f32,
});
expect(tgpu.resolve([s1], { names: 'strict' })).toContain('@builtin(position) position: vec4f');
});
});

Expand Down
Loading
Loading