diff --git a/apps/typegpu-docs/tests/individual-example-tests/oklab.test.ts b/apps/typegpu-docs/tests/individual-example-tests/oklab.test.ts index b8e64d3654..b3d9a61186 100644 --- a/apps/typegpu-docs/tests/individual-example-tests/oklab.test.ts +++ b/apps/typegpu-docs/tests/individual-example-tests/oklab.test.ts @@ -182,11 +182,11 @@ describe('oklab example', () => { let g2 = (((-1.2684380046f * ldt2) + (2.6097574011f * mdt2)) - (0.3413193965f * sdt2)); let u_g = (g1 / ((g1 * g1) - ((0.5f * g) * g2))); var t_g = (-(g) * u_g); - let b_1 = ((((-0.0041960863f * l) - (0.7034186147f * m)) + (1.707614701f * s)) - 1f); + let b2 = ((((-0.0041960863f * l) - (0.7034186147f * m)) + (1.707614701f * s)) - 1f); let b1 = (((-0.0041960863f * ldt) - (0.7034186147f * mdt)) + (1.707614701f * sdt)); - let b2 = (((-0.0041960863f * ldt2) - (0.7034186147f * mdt2)) + (1.707614701f * sdt2)); - let u_b = (b1 / ((b1 * b1) - ((0.5f * b_1) * b2))); - var t_b = (-(b_1) * u_b); + let b22 = (((-0.0041960863f * ldt2) - (0.7034186147f * mdt2)) + (1.707614701f * sdt2)); + let u_b = (b1 / ((b1 * b1) - ((0.5f * b2) * b22))); + var t_b = (-(b2) * u_b); t_r = select(FLT_MAX, t_r, (u_r >= 0f)); t_g = select(FLT_MAX, t_g, (u_g >= 0f)); t_b = select(FLT_MAX, t_b, (u_b >= 0f)); diff --git a/package.json b/package.json index 9dd5762786..218ba3d663 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dev": "DEV=true pnpm run --filter typegpu-docs dev", "dev:host": "DEV=true pnpm run --filter typegpu-docs dev --host --mode https", "fix": "oxlint -c oxlint.config.ts --fix && oxfmt", - "test": "pnpm run test:types && pnpm run test:style && pnpm run test:unit-and-attest && pnpm run test:circular-deps", + "test": "pnpm -r run build && pnpm run test:types && pnpm run test:style && TEST_BUILT_CODE=true pnpm run test:unit-and-attest && pnpm run test:circular-deps", "test:circular-deps": "pnpm dpdm -T --exit-code circular:1 packages/**/src/index.ts packages/**/src/index.js !packages/**/node_modules", "test:types": "pnpm run --filter typegpu-docs transform-overloads && pnpm run -r --parallel test:types", "test:style": "oxlint -c oxlint.config.ts --max-warnings=0 --type-aware --report-unused-disable-directives && oxfmt --check", diff --git a/packages/typegpu/src/tgsl/shaderGenerator_members.ts b/packages/typegpu/src/tgsl/shaderGenerator_members.ts index a33ac92b24..5d29b6bc5e 100644 --- a/packages/typegpu/src/tgsl/shaderGenerator_members.ts +++ b/packages/typegpu/src/tgsl/shaderGenerator_members.ts @@ -2,3 +2,4 @@ export { UnknownData } from '../data/dataTypes.ts'; // types export type { ResolutionCtx } from '../types.ts'; +export type { Snippet, Origin } from '../data/snippet.ts'; diff --git a/packages/typegpu/src/tgsl/wgslGenerator.ts b/packages/typegpu/src/tgsl/wgslGenerator.ts index 7c7c06d7c8..fc413e08fe 100644 --- a/packages/typegpu/src/tgsl/wgslGenerator.ts +++ b/packages/typegpu/src/tgsl/wgslGenerator.ts @@ -910,6 +910,75 @@ ${this.ctx.pre}}`; return snip(stitch`${this.ctx.resolve(schema).value}(${args})`, schema, 'runtime'); } + public _return(statement: tinyest.Return): string { + const returnNode = statement[1]; + + if (returnNode !== undefined) { + const expectedReturnType = this.ctx.topFunctionReturnType; + let returnSnippet = expectedReturnType + ? this._typedExpression(returnNode, expectedReturnType) + : this._expression(returnNode); + + if (returnSnippet.value instanceof RefOperator) { + throw new WgslTypeError( + stitch`Cannot return references, returning '${returnSnippet.value.snippet}'`, + ); + } + + // Arguments cannot be returned from functions without copying. A simple example why is: + // const identity = (x) => { + // 'use gpu'; + // return x; + // }; + // + // const foo = (arg: d.v3f) => { + // 'use gpu'; + // const marg = identity(arg); + // marg.x = 1; // 'marg's origin would be 'runtime', so we wouldn't be able to track this misuse. + // }; + if ( + returnSnippet.origin === 'argument' && + !wgsl.isNaturallyEphemeral(returnSnippet.dataType) && + // Only restricting this use in non-entry functions, as the function + // is giving up ownership of all references anyway. + this.ctx.topFunctionScope?.functionType === 'normal' + ) { + throw new WgslTypeError( + stitch`Cannot return references to arguments, returning '${returnSnippet}'. Copy the argument before returning it.`, + ); + } + + if ( + !expectedReturnType && + !isEphemeralSnippet(returnSnippet) && + returnSnippet.origin !== 'this-function' + ) { + const str = this.ctx.resolve(returnSnippet.value, returnSnippet.dataType).value; + const typeStr = this.ctx.resolve(unptr(returnSnippet.dataType)).value; + throw new WgslTypeError( + `'return ${str};' is invalid, cannot return references. +----- +Try 'return ${typeStr}(${str});' instead. +-----`, + ); + } + + returnSnippet = tryConvertSnippet( + this.ctx, + returnSnippet, + unptr(returnSnippet.dataType) as wgsl.AnyWgslData, + false, + ); + + invariant(returnSnippet.dataType !== UnknownData, 'Return type should be known'); + + this.ctx.reportReturnType(returnSnippet.dataType); + return stitch`${this.ctx.pre}return ${returnSnippet};`; + } + + return `${this.ctx.pre}return;`; + } + public _statement(statement: tinyest.Statement): string { if (typeof statement === 'string') { const id = this._identifier(statement); @@ -923,72 +992,7 @@ ${this.ctx.pre}}`; } if (statement[0] === NODE.return) { - const returnNode = statement[1]; - - if (returnNode !== undefined) { - const expectedReturnType = this.ctx.topFunctionReturnType; - let returnSnippet = expectedReturnType - ? this._typedExpression(returnNode, expectedReturnType) - : this._expression(returnNode); - - if (returnSnippet.value instanceof RefOperator) { - throw new WgslTypeError( - stitch`Cannot return references, returning '${returnSnippet.value.snippet}'`, - ); - } - - // Arguments cannot be returned from functions without copying. A simple example why is: - // const identity = (x) => { - // 'use gpu'; - // return x; - // }; - // - // const foo = (arg: d.v3f) => { - // 'use gpu'; - // const marg = identity(arg); - // marg.x = 1; // 'marg's origin would be 'runtime', so we wouldn't be able to track this misuse. - // }; - if ( - returnSnippet.origin === 'argument' && - !wgsl.isNaturallyEphemeral(returnSnippet.dataType) && - // Only restricting this use in non-entry functions, as the function - // is giving up ownership of all references anyway. - this.ctx.topFunctionScope?.functionType === 'normal' - ) { - throw new WgslTypeError( - stitch`Cannot return references to arguments, returning '${returnSnippet}'. Copy the argument before returning it.`, - ); - } - - if ( - !expectedReturnType && - !isEphemeralSnippet(returnSnippet) && - returnSnippet.origin !== 'this-function' - ) { - const str = this.ctx.resolve(returnSnippet.value, returnSnippet.dataType).value; - const typeStr = this.ctx.resolve(unptr(returnSnippet.dataType)).value; - throw new WgslTypeError( - `'return ${str};' is invalid, cannot return references. ------ -Try 'return ${typeStr}(${str});' instead. ------`, - ); - } - - returnSnippet = tryConvertSnippet( - this.ctx, - returnSnippet, - unptr(returnSnippet.dataType) as wgsl.AnyWgslData, - false, - ); - - invariant(returnSnippet.dataType !== UnknownData, 'Return type should be known'); - - this.ctx.reportReturnType(returnSnippet.dataType); - return stitch`${this.ctx.pre}return ${returnSnippet};`; - } - - return `${this.ctx.pre}return;`; + return this._return(statement); } if (statement[0] === NODE.if) { diff --git a/packages/typegpu/tests/accessor.test.ts b/packages/typegpu/tests/accessor.test.ts index 851263eb88..bc425d3ead 100644 --- a/packages/typegpu/tests/accessor.test.ts +++ b/packages/typegpu/tests/accessor.test.ts @@ -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); diff --git a/packages/typegpu/tests/align.test.ts b/packages/typegpu/tests/align.test.ts index 67804585ae..f52ec08222 100644 --- a/packages/typegpu/tests/align.test.ts +++ b/packages/typegpu/tests/align.test.ts @@ -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', () => { diff --git a/packages/typegpu/tests/array.test.ts b/packages/typegpu/tests/array.test.ts index 0eb30bbd72..564b94dbd9 100644 --- a/packages/typegpu/tests/array.test.ts +++ b/packages/typegpu/tests/array.test.ts @@ -2,7 +2,7 @@ import { attest } from '@ark/attest'; import { BufferReader, BufferWriter } from 'typed-binary'; import { describe, expect, expectTypeOf, it } from 'vitest'; import { readData, writeData } from '../src/data/dataIO.ts'; -import { d, tgpu } from '../src/index.js'; +import { d, tgpu } from 'typegpu'; import { namespace } from '../src/core/resolve/namespace.ts'; import { resolve } from '../src/resolutionCtx.ts'; import type { Infer } from '../src/shared/repr.ts'; diff --git a/packages/typegpu/tests/attributes.test.ts b/packages/typegpu/tests/attributes.test.ts index 97bf76592c..90ebda9d1b 100644 --- a/packages/typegpu/tests/attributes.test.ts +++ b/packages/typegpu/tests/attributes.test.ts @@ -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', () => { diff --git a/packages/typegpu/tests/bindGroupLayout.test.ts b/packages/typegpu/tests/bindGroupLayout.test.ts index b7fe5d5a81..87370af360 100644 --- a/packages/typegpu/tests/bindGroupLayout.test.ts +++ b/packages/typegpu/tests/bindGroupLayout.test.ts @@ -6,7 +6,7 @@ import { type TgpuBuffer, type TgpuTextureView, type UniformFlag, -} from '../src/index.js'; +} from 'typegpu'; import { type ExtractBindGroupInputFromLayout, MissingBindingError, diff --git a/packages/typegpu/tests/buffer.test.ts b/packages/typegpu/tests/buffer.test.ts index f1938683f4..93283f108c 100644 --- a/packages/typegpu/tests/buffer.test.ts +++ b/packages/typegpu/tests/buffer.test.ts @@ -3,7 +3,7 @@ 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 type { ValidateBufferSchema, ValidUsagesFor } from 'typegpu'; 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'; diff --git a/packages/typegpu/tests/bufferShorthands.test.ts b/packages/typegpu/tests/bufferShorthands.test.ts index 7f0301e963..8a8ced6283 100644 --- a/packages/typegpu/tests/bufferShorthands.test.ts +++ b/packages/typegpu/tests/bufferShorthands.test.ts @@ -8,7 +8,7 @@ import type { TgpuReadonly, TgpuUniform, UniformFlag, -} from '../src/index.js'; +} from 'typegpu'; import { attest } from '@ark/attest'; describe('root.createMutable', () => { diff --git a/packages/typegpu/tests/bufferUsage.test.ts b/packages/typegpu/tests/bufferUsage.test.ts index d3ca51e60b..d9afdfaa79 100644 --- a/packages/typegpu/tests/bufferUsage.test.ts +++ b/packages/typegpu/tests/bufferUsage.test.ts @@ -1,6 +1,6 @@ import { describe, expect, expectTypeOf } from 'vitest'; -import { d, tgpu } from '../src/index.js'; +import { d, tgpu } from 'typegpu'; import type { Infer } from '../src/shared/repr.ts'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/computePipeline.test.ts b/packages/typegpu/tests/computePipeline.test.ts index 6fadc72872..36d210b128 100644 --- a/packages/typegpu/tests/computePipeline.test.ts +++ b/packages/typegpu/tests/computePipeline.test.ts @@ -1,6 +1,6 @@ import { describe, expect, expectTypeOf, vi } from 'vitest'; import type { TgpuQuerySet } from '../src/core/querySet/querySet.ts'; -import { d, MissingBindGroupsError, tgpu, type TgpuComputePipeline } from '../src/index.js'; +import { d, MissingBindGroupsError, tgpu, type TgpuComputePipeline } from 'typegpu'; import { $internal } from '../src/shared/symbols.ts'; import { it } from 'typegpu-testing-utility'; import { extensionEnabled } from '../src/std/extensions.ts'; diff --git a/packages/typegpu/tests/constant.test.ts b/packages/typegpu/tests/constant.test.ts index 707aa9ef2e..38cfd1ea01 100644 --- a/packages/typegpu/tests/constant.test.ts +++ b/packages/typegpu/tests/constant.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { d, tgpu } from '../src/index.js'; +import { d, tgpu } from 'typegpu'; const Boid = d.struct({ pos: d.vec3f, diff --git a/packages/typegpu/tests/data/atomic.test.ts b/packages/typegpu/tests/data/atomic.test.ts index ba2915993b..76e4286a28 100644 --- a/packages/typegpu/tests/data/atomic.test.ts +++ b/packages/typegpu/tests/data/atomic.test.ts @@ -1,5 +1,5 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; -import * as d from '../../src/data/index.ts'; +import { d } from 'typegpu'; describe('d.atomic', () => { it('creates a u32 atomic schema', () => { diff --git a/packages/typegpu/tests/data/deepEqual.test.ts b/packages/typegpu/tests/data/deepEqual.test.ts index f994e2c9eb..55602a1660 100644 --- a/packages/typegpu/tests/data/deepEqual.test.ts +++ b/packages/typegpu/tests/data/deepEqual.test.ts @@ -21,8 +21,10 @@ import { vec2f, vec2u, vec3f, -} from '../../src/data/index.ts'; -import { ptrPrivate, ptrStorage, ptrWorkgroup } from '../../src/data/ptr.ts'; + ptrPrivate, + ptrStorage, + ptrWorkgroup, +} from 'typegpu/data'; describe('deepEqual', () => { it('compares simple types', () => { diff --git a/packages/typegpu/tests/data/ptr.test.ts b/packages/typegpu/tests/data/ptr.test.ts index 12c9921127..b4228cae37 100644 --- a/packages/typegpu/tests/data/ptr.test.ts +++ b/packages/typegpu/tests/data/ptr.test.ts @@ -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.ptrFn', () => { it('wraps a schema and infers type properly', () => { diff --git a/packages/typegpu/tests/data/schemaCallWrapper.test.ts b/packages/typegpu/tests/data/schemaCallWrapper.test.ts index 6bedaba098..5e7defda4b 100644 --- a/packages/typegpu/tests/data/schemaCallWrapper.test.ts +++ b/packages/typegpu/tests/data/schemaCallWrapper.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import * as d from '../../src/data/index.ts'; +import { d } from 'typegpu'; import { schemaCallWrapper } from '../../src/data/schemaCallWrapper.ts'; describe('schemaCallWrapper', () => { diff --git a/packages/typegpu/tests/declare.test.ts b/packages/typegpu/tests/declare.test.ts index a4247ba832..2b08d5c93b 100644 --- a/packages/typegpu/tests/declare.test.ts +++ b/packages/typegpu/tests/declare.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { d, tgpu } from '../src/index.js'; +import { d, tgpu } from 'typegpu'; describe('tgpu.declare', () => { it('should inject provided declaration when resolving a function', () => { diff --git a/packages/typegpu/tests/entryFnBuiltinArgs.test.ts b/packages/typegpu/tests/entryFnBuiltinArgs.test.ts index d064eb9148..da7888a4b0 100644 --- a/packages/typegpu/tests/entryFnBuiltinArgs.test.ts +++ b/packages/typegpu/tests/entryFnBuiltinArgs.test.ts @@ -1,6 +1,6 @@ import { describe, it } from 'vitest'; import * as d from '../src/data/index.ts'; -import tgpu, { type TgpuComputeFn, type TgpuFragmentFn, type TgpuVertexFn } from '../src/index.js'; +import tgpu, { type TgpuComputeFn, type TgpuFragmentFn, type TgpuVertexFn } from 'typegpu'; import { attest } from '@ark/attest'; describe('entry functions accepting only the allowed subset of builtins', () => { diff --git a/packages/typegpu/tests/entryFnHeaderGen.test.ts b/packages/typegpu/tests/entryFnHeaderGen.test.ts index a0b9abfa69..a1fe6757f1 100644 --- a/packages/typegpu/tests/entryFnHeaderGen.test.ts +++ b/packages/typegpu/tests/entryFnHeaderGen.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import * as d from '../src/data/index.ts'; -import tgpu from '../src/index.js'; +import tgpu from 'typegpu'; describe('autogenerating wgsl headers for tgpu entry functions with raw string WGSL implementations', () => { it('works for fragment entry function with non-decorated non-struct output', () => { diff --git a/packages/typegpu/tests/function.test.ts b/packages/typegpu/tests/function.test.ts index 0940c5045a..0c5892c597 100644 --- a/packages/typegpu/tests/function.test.ts +++ b/packages/typegpu/tests/function.test.ts @@ -3,7 +3,7 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; import type { InferIO, InheritArgNames, IOLayout } from '../src/core/function/fnTypes.ts'; import * as d from '../src/data/index.ts'; import { Void } from '../src/data/wgslTypes.ts'; -import tgpu, { type TgpuFn, type TgpuFnShell } from '../src/index.js'; +import tgpu, { type TgpuFn, type TgpuFnShell } from 'typegpu'; import type { Prettify } from '../src/shared/utilityTypes.ts'; const empty = tgpu.fn([])`() { diff --git a/packages/typegpu/tests/functionTagged.test.ts b/packages/typegpu/tests/functionTagged.test.ts index 6d86b8c85d..8944104f97 100644 --- a/packages/typegpu/tests/functionTagged.test.ts +++ b/packages/typegpu/tests/functionTagged.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import * as d from '../src/data/index.ts'; -import tgpu from '../src/index.js'; +import tgpu from 'typegpu'; describe('tagged syntax', () => { describe('function', () => { diff --git a/packages/typegpu/tests/gpuValueOf.test.ts b/packages/typegpu/tests/gpuValueOf.test.ts index 9c4a87abc3..607057ae6b 100644 --- a/packages/typegpu/tests/gpuValueOf.test.ts +++ b/packages/typegpu/tests/gpuValueOf.test.ts @@ -1,6 +1,6 @@ import { describe, expectTypeOf } from 'vitest'; import * as d from '../src/data/index.ts'; -import tgpu from '../src/index.js'; +import tgpu from 'typegpu'; import type { GPUValueOf } from '../src/shared/repr.ts'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/indent.test.ts b/packages/typegpu/tests/indent.test.ts index 2952762fcf..32b1f202ce 100644 --- a/packages/typegpu/tests/indent.test.ts +++ b/packages/typegpu/tests/indent.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d, std } from '../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('indents', () => { it('should indent sanely', () => { diff --git a/packages/typegpu/tests/tgsl/conversion.test.ts b/packages/typegpu/tests/internal/conversion.test.ts similarity index 99% rename from packages/typegpu/tests/tgsl/conversion.test.ts rename to packages/typegpu/tests/internal/conversion.test.ts index ee72863801..c34def9747 100644 --- a/packages/typegpu/tests/tgsl/conversion.test.ts +++ b/packages/typegpu/tests/internal/conversion.test.ts @@ -1,4 +1,5 @@ import { afterAll, beforeAll, describe, expect } from 'vitest'; +// Importing directly from source, since we're testing internals import { abstractFloat, abstractInt } from '../../src/data/numeric.ts'; import * as d from '../../src/data/index.ts'; import { snip, type Snippet } from '../../src/data/snippet.ts'; diff --git a/packages/typegpu/tests/internal/deserializeAndStringify.test.ts b/packages/typegpu/tests/internal/deserializeAndStringify.test.ts new file mode 100644 index 0000000000..77318ffb1c --- /dev/null +++ b/packages/typegpu/tests/internal/deserializeAndStringify.test.ts @@ -0,0 +1,133 @@ +import { describe, expect } from 'vitest'; +// Importing directly from source, since we're testing internals +import { deserializeAndStringify } from '../../src/tgsl/consoleLog/deserializers.ts'; +import { d } from '../../src/index.js'; +import { it } from 'typegpu-testing-utility'; + +describe('deserializeAndStringify', () => { + it('works for string literals', () => { + const data = new Uint32Array([]); + const logInfo: (string | d.AnyWgslData)[] = ['String literal']; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "String literal", + ] + `, + ); + }); + + it('works for u32', () => { + const data = new Uint32Array([123]); + const logInfo: (string | d.AnyWgslData)[] = [d.u32]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "123", + ] + `, + ); + }); + + it('works for vec3u', () => { + const data = new Uint32Array([1, 2, 3]); + const logInfo: (string | d.AnyWgslData)[] = [d.vec3u]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "vec3u(1, 2, 3)", + ] + `, + ); + }); + + it('works for clumped vectors', () => { + const data = new Uint32Array([1, 2, 3, 4, 5, 6]); // no alignment + const logInfo: (string | d.AnyWgslData)[] = [d.vec3u, d.vec3u]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "vec3u(1, 2, 3)", + "vec3u(4, 5, 6)", + ] + `, + ); + }); + + it('works for multiple arguments', () => { + const data = new Uint32Array([1, 2, 3, 456]); + const logInfo: (string | d.AnyWgslData)[] = ['GID:', d.vec3u, 'Result:', d.u32]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "GID:", + "vec3u(1, 2, 3)", + "Result:", + "456", + ] + `, + ); + }); + + it('works for arrays', () => { + const data = new Uint32Array([1, 2, 3, 4]); + const logInfo: (string | d.AnyWgslData)[] = [d.arrayOf(d.u32, 4)]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "[1, 2, 3, 4]", + ] + `, + ); + }); + + it('works for nested arrays', () => { + const data = new Uint32Array([1, 2, 3, 4]); + const logInfo: (string | d.AnyWgslData)[] = [d.arrayOf(d.arrayOf(d.u32, 2), 2)]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "[[1, 2], [3, 4]]", + ] + `, + ); + }); + + it('works for structs', () => { + const data = new Uint32Array([1, 2, 3, 4]); + const logInfo: (string | d.AnyWgslData)[] = [d.struct({ vec: d.vec3u, num: d.u32 })]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "{ vec: vec3u(1, 2, 3), num: 4 }", + ] + `, + ); + }); + + it('works for nested structs', () => { + const data = new Uint32Array([1, 2, 3, 4, 1]); + const logInfo: (string | d.AnyWgslData)[] = [ + d.struct({ + nested: d.struct({ vec: d.vec3u, num: d.u32 }), + bool: d.bool, + }), + ]; + + expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( + ` + [ + "{ nested: { vec: vec3u(1, 2, 3), num: 4 }, bool: true }", + ] + `, + ); + }); +}); diff --git a/packages/typegpu/tests/tgsl/dualImpl.test.ts b/packages/typegpu/tests/internal/dualImpl.test.ts similarity index 98% rename from packages/typegpu/tests/tgsl/dualImpl.test.ts rename to packages/typegpu/tests/internal/dualImpl.test.ts index 758b14befb..5dba380984 100644 --- a/packages/typegpu/tests/tgsl/dualImpl.test.ts +++ b/packages/typegpu/tests/internal/dualImpl.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from 'vitest'; +// Importing directly from source, since we're testing internals import { dualImpl, MissingCpuImplError } from '../../src/core/function/dualImpl.ts'; import { Void } from '../../src/data/wgslTypes.ts'; import tgpu from '../../src/index.js'; diff --git a/packages/typegpu/tests/tgsl/generationHelpers.test.ts b/packages/typegpu/tests/internal/generationHelpers.test.ts similarity index 99% rename from packages/typegpu/tests/tgsl/generationHelpers.test.ts rename to packages/typegpu/tests/internal/generationHelpers.test.ts index 9a51b702c3..0a7b32c961 100644 --- a/packages/typegpu/tests/tgsl/generationHelpers.test.ts +++ b/packages/typegpu/tests/internal/generationHelpers.test.ts @@ -1,4 +1,5 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +// Importing directly from source, since we're testing internals import { arrayOf } from '../../src/data/array.ts'; import { mat2x2f, mat3x3f, mat4x4f } from '../../src/data/matrix.ts'; import { abstractFloat, abstractInt, bool, f16, f32, i32, u32 } from '../../src/data/numeric.ts'; diff --git a/packages/typegpu/tests/invariant.test.ts b/packages/typegpu/tests/invariant.test.ts index 2a83ef94dc..e87613ba33 100644 --- a/packages/typegpu/tests/invariant.test.ts +++ b/packages/typegpu/tests/invariant.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('invariant', () => { it('adds @invariant attribute to position builtin', () => { diff --git a/packages/typegpu/tests/jsMath.test.ts b/packages/typegpu/tests/jsMath.test.ts index a0f08e0410..724033bcee 100644 --- a/packages/typegpu/tests/jsMath.test.ts +++ b/packages/typegpu/tests/jsMath.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('Math', () => { it('allows using Math.PI', () => { diff --git a/packages/typegpu/tests/lazy.test.ts b/packages/typegpu/tests/lazy.test.ts index 5058c537e2..4e0a209bbd 100644 --- a/packages/typegpu/tests/lazy.test.ts +++ b/packages/typegpu/tests/lazy.test.ts @@ -1,6 +1,6 @@ import { describe, expect, expectTypeOf, vi } from 'vitest'; import * as d from '../src/data/index.ts'; -import tgpu, { type TgpuLazy } from '../src/index.js'; +import tgpu, { type TgpuLazy } from 'typegpu'; import { mul } from '../src/std/index.ts'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/limitsOverflow.test.ts b/packages/typegpu/tests/limitsOverflow.test.ts index f0630e8ee5..cf3a31897f 100644 --- a/packages/typegpu/tests/limitsOverflow.test.ts +++ b/packages/typegpu/tests/limitsOverflow.test.ts @@ -1,6 +1,6 @@ import { describe, expect, vi } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { warnIfOverflow } from '../src/core/pipeline/limitsOverflow.ts'; describe('warnIfOverflow', () => { diff --git a/packages/typegpu/tests/matrix.test.ts b/packages/typegpu/tests/matrix.test.ts index 989f6f4c8f..3d20449e6b 100644 --- a/packages/typegpu/tests/matrix.test.ts +++ b/packages/typegpu/tests/matrix.test.ts @@ -1,6 +1,6 @@ import { BufferReader, BufferWriter } from 'typed-binary'; import { describe, expect, expectTypeOf, it } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { readData, writeData } from '../src/data/dataIO.ts'; import { isCloseTo } from '../src/std/index.ts'; diff --git a/packages/typegpu/tests/namespace.test.ts b/packages/typegpu/tests/namespace.test.ts index ee6fe33099..aa9a6f53f4 100644 --- a/packages/typegpu/tests/namespace.test.ts +++ b/packages/typegpu/tests/namespace.test.ts @@ -1,5 +1,5 @@ import { describe, expect, vi } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('tgpu.namespace', () => { diff --git a/packages/typegpu/tests/numeric.test.ts b/packages/typegpu/tests/numeric.test.ts index 71c66278ee..1350ac3f4f 100644 --- a/packages/typegpu/tests/numeric.test.ts +++ b/packages/typegpu/tests/numeric.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('f32', () => { it('differs in type from other numeric schemas', () => { diff --git a/packages/typegpu/tests/offsetUtils.test.ts b/packages/typegpu/tests/offsetUtils.test.ts index 96c2409d5f..4efc6a961d 100644 --- a/packages/typegpu/tests/offsetUtils.test.ts +++ b/packages/typegpu/tests/offsetUtils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { d } from '../src/index.js'; +import { d } from 'typegpu'; import { sizeOf } from '../src/data/sizeOf.ts'; describe('d.memoryLayoutOf (default)', () => { diff --git a/packages/typegpu/tests/pipeline-resolution.test.ts b/packages/typegpu/tests/pipeline-resolution.test.ts index 3d5272ae8f..9f92bb0b3a 100644 --- a/packages/typegpu/tests/pipeline-resolution.test.ts +++ b/packages/typegpu/tests/pipeline-resolution.test.ts @@ -1,5 +1,5 @@ import { describe, expect } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('resolve', () => { diff --git a/packages/typegpu/tests/rawFn.test.ts b/packages/typegpu/tests/rawFn.test.ts index b63036de3b..9faa28ea29 100644 --- a/packages/typegpu/tests/rawFn.test.ts +++ b/packages/typegpu/tests/rawFn.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { getName } from '../src/shared/meta.ts'; describe('tgpu.fn with raw string WGSL implementation', () => { diff --git a/packages/typegpu/tests/ref.test.ts b/packages/typegpu/tests/ref.test.ts index 4eb9f5b1b4..198686486c 100644 --- a/packages/typegpu/tests/ref.test.ts +++ b/packages/typegpu/tests/ref.test.ts @@ -1,4 +1,4 @@ -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/renderPipeline.test.ts b/packages/typegpu/tests/renderPipeline.test.ts index 316f2d0e02..8b280884f4 100644 --- a/packages/typegpu/tests/renderPipeline.test.ts +++ b/packages/typegpu/tests/renderPipeline.test.ts @@ -11,7 +11,7 @@ import tgpu, { type TgpuRenderPipeline, type TgpuVertexFn, type TgpuVertexFnShell, -} from '../src/index.js'; +} from 'typegpu'; import { $internal } from '../src/shared/symbols.ts'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/resolve.test.ts b/packages/typegpu/tests/resolve.test.ts index 254730def2..17427294f8 100644 --- a/packages/typegpu/tests/resolve.test.ts +++ b/packages/typegpu/tests/resolve.test.ts @@ -1,5 +1,5 @@ import { describe, expect, vi } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { setName } from '../src/shared/meta.ts'; import { $gpuValueOf, $internal, $ownSnippet, $resolve } from '../src/shared/symbols.ts'; import type { ResolutionCtx } from '../src/types.ts'; diff --git a/packages/typegpu/tests/root.test.ts b/packages/typegpu/tests/root.test.ts index 525e9a0007..cce511a092 100644 --- a/packages/typegpu/tests/root.test.ts +++ b/packages/typegpu/tests/root.test.ts @@ -1,6 +1,6 @@ import { describe, expect, vi } from 'vitest'; import { Void } from '../src/data/wgslTypes.ts'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('TgpuRoot', () => { diff --git a/packages/typegpu/tests/simulate.test.ts b/packages/typegpu/tests/simulate.test.ts index b3310f1f71..492f6a99ad 100644 --- a/packages/typegpu/tests/simulate.test.ts +++ b/packages/typegpu/tests/simulate.test.ts @@ -1,5 +1,5 @@ import { describe, expect } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('tgpu.simulate()', () => { diff --git a/packages/typegpu/tests/slot.test.ts b/packages/typegpu/tests/slot.test.ts index 8f1fd76175..d2032ff0ac 100644 --- a/packages/typegpu/tests/slot.test.ts +++ b/packages/typegpu/tests/slot.test.ts @@ -1,5 +1,5 @@ import { describe, expect } from 'vitest'; -import tgpu, { d, std } from '../src/index.js'; +import tgpu, { d, std } from 'typegpu'; import { it } from 'typegpu-testing-utility'; import { getName } from '../src/shared/meta.ts'; diff --git a/packages/typegpu/tests/std/bitcast.test.ts b/packages/typegpu/tests/std/bitcast.test.ts index dabcd382a4..948964b270 100644 --- a/packages/typegpu/tests/std/bitcast.test.ts +++ b/packages/typegpu/tests/std/bitcast.test.ts @@ -1,16 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { - vec2f, - vec2i, - vec2u, - vec3f, - vec3i, - vec3u, - vec4f, - vec4i, - vec4u, -} from '../../src/data/vector.ts'; -import tgpu, { d, std } from '../../src/index.js'; +import { vec2f, vec2i, vec2u, vec3f, vec3i, vec3u, vec4f, vec4i, vec4u } from 'typegpu/data'; +import tgpu, { d, std } from 'typegpu'; describe('bitcast', () => { it('bitcastU32toF32', () => { diff --git a/packages/typegpu/tests/std/boolean/not.test.ts b/packages/typegpu/tests/std/boolean/not.test.ts index 930c405ccd..794308626d 100644 --- a/packages/typegpu/tests/std/boolean/not.test.ts +++ b/packages/typegpu/tests/std/boolean/not.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; import { not } from '../../../src/std/boolean.ts'; -import tgpu, { d, std } from '../../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('not', () => { it('negates booleans', () => { diff --git a/packages/typegpu/tests/std/boolean/select.test.ts b/packages/typegpu/tests/std/boolean/select.test.ts index 3c1bdc908a..c226f24516 100644 --- a/packages/typegpu/tests/std/boolean/select.test.ts +++ b/packages/typegpu/tests/std/boolean/select.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d } from '../../../src/index.js'; +import tgpu, { d } from 'typegpu'; import { vec2b, vec2f, diff --git a/packages/typegpu/tests/std/matrix/rotate.test.ts b/packages/typegpu/tests/std/matrix/rotate.test.ts index 191aa4d3ab..a1803da17e 100644 --- a/packages/typegpu/tests/std/matrix/rotate.test.ts +++ b/packages/typegpu/tests/std/matrix/rotate.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import { mat4x4f, vec4f } from '../../../src/data/index.ts'; -import tgpu from '../../../src/index.js'; +import tgpu from 'typegpu'; import { isCloseTo, mul } from '../../../src/std/index.ts'; import { rotateX4, rotateY4, rotateZ4 } from '../../../src/std/matrix.ts'; diff --git a/packages/typegpu/tests/std/matrix/scale.test.ts b/packages/typegpu/tests/std/matrix/scale.test.ts index 89907e7ec8..6b38d95f52 100644 --- a/packages/typegpu/tests/std/matrix/scale.test.ts +++ b/packages/typegpu/tests/std/matrix/scale.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import { mat4x4f, vec3f, vec4f } from '../../../src/data/index.ts'; -import tgpu from '../../../src/index.js'; +import tgpu from 'typegpu'; import { isCloseTo, mul, scale4, translate4 } from '../../../src/std/index.ts'; describe('scale', () => { diff --git a/packages/typegpu/tests/std/matrix/translate.test.ts b/packages/typegpu/tests/std/matrix/translate.test.ts index 20b3998551..96fbe10753 100644 --- a/packages/typegpu/tests/std/matrix/translate.test.ts +++ b/packages/typegpu/tests/std/matrix/translate.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import { mat4x4f, vec3f, vec4f } from '../../../src/data/index.ts'; -import tgpu from '../../../src/index.js'; +import tgpu from 'typegpu'; import { isCloseTo, mul, scale4, translate4 } from '../../../src/std/index.ts'; describe('translate', () => { diff --git a/packages/typegpu/tests/std/numeric/acos.test.ts b/packages/typegpu/tests/std/numeric/acos.test.ts index 0b08a76027..dc17ce2366 100644 --- a/packages/typegpu/tests/std/numeric/acos.test.ts +++ b/packages/typegpu/tests/std/numeric/acos.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec3f } from '../../../src/data/index.ts'; -import { acos, isCloseTo } from '../../../src/std/index.ts'; +import { vec3f } from 'typegpu/data'; +import { acos, isCloseTo } from 'typegpu/std'; describe('acos', () => { it('computes acos of numeric value', () => { diff --git a/packages/typegpu/tests/std/numeric/acosh.test.ts b/packages/typegpu/tests/std/numeric/acosh.test.ts index 7a052fe858..b99913137f 100644 --- a/packages/typegpu/tests/std/numeric/acosh.test.ts +++ b/packages/typegpu/tests/std/numeric/acosh.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { acosh, isCloseTo } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { acosh, isCloseTo } from 'typegpu/std'; describe('acosh', () => { it('computes acosh of a number', () => { diff --git a/packages/typegpu/tests/std/numeric/add.test.ts b/packages/typegpu/tests/std/numeric/add.test.ts index cbd5a2184b..473b05e21c 100644 --- a/packages/typegpu/tests/std/numeric/add.test.ts +++ b/packages/typegpu/tests/std/numeric/add.test.ts @@ -1,5 +1,5 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; -import type { m3x3f, v2f, v3f, v4f } from '../../../src/data/index.ts'; +import type { m3x3f, v2f, v3f, v4f } from 'typegpu/data'; import { mat2x2f, mat3x3f, @@ -13,10 +13,9 @@ import { vec4f, vec4i, vec4u, -} from '../../../src/data/index.ts'; -import { add } from '../../../src/std/index.ts'; +} from 'typegpu/data'; +import { add } from 'typegpu/std'; import { expectDataTypeOf } from '../../utils/parseResolved.ts'; -import { abstractFloat, abstractInt } from '../../../src/data/numeric.ts'; describe('add', () => { it('computes sum of two vec2f', () => { @@ -104,28 +103,28 @@ describe('add', () => { it('infers types when adding constants', () => { const int_int = () => { 'use gpu'; - 1 + 2; + return 1 + 2; }; const float_float = () => { 'use gpu'; - 1.1 + 2.3; + return 1.1 + 2.3; }; const int_float = () => { 'use gpu'; - 1.1 + 2; + return 1.1 + 2; }; const float_int = () => { 'use gpu'; - 1 + 2.3; + return 1 + 2.3; }; - expectDataTypeOf(int_int).toBe(abstractInt); - expectDataTypeOf(float_float).toBe(abstractFloat); - expectDataTypeOf(int_float).toBe(abstractFloat); - expectDataTypeOf(float_int).toBe(abstractFloat); + expectDataTypeOf(int_int).toMatchObject({ type: 'abstractInt' }); + expectDataTypeOf(float_float).toMatchObject({ type: 'abstractFloat' }); + expectDataTypeOf(int_float).toMatchObject({ type: 'abstractFloat' }); + expectDataTypeOf(float_int).toMatchObject({ type: 'abstractFloat' }); }); }); }); diff --git a/packages/typegpu/tests/std/numeric/asin.test.ts b/packages/typegpu/tests/std/numeric/asin.test.ts index f2647c0502..7e88b4eb4a 100644 --- a/packages/typegpu/tests/std/numeric/asin.test.ts +++ b/packages/typegpu/tests/std/numeric/asin.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec3f } from '../../../src/data/index.ts'; -import { asin, isCloseTo } from '../../../src/std/index.ts'; +import { vec3f } from 'typegpu/data'; +import { asin, isCloseTo } from 'typegpu/std'; describe('asin', () => { it('computes asin of numeric value', () => { diff --git a/packages/typegpu/tests/std/numeric/atan2.test.ts b/packages/typegpu/tests/std/numeric/atan2.test.ts index fd28aa7fd4..11cbc60a5a 100644 --- a/packages/typegpu/tests/std/numeric/atan2.test.ts +++ b/packages/typegpu/tests/std/numeric/atan2.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec4f } from '../../../src/data/index.ts'; -import { atan2, isCloseTo } from '../../../src/std/index.ts'; +import { vec4f } from 'typegpu/data'; +import { atan2, isCloseTo } from 'typegpu/std'; describe('atan2', () => { it('computes atan2 of two values', () => { diff --git a/packages/typegpu/tests/std/numeric/bitShift.test.ts b/packages/typegpu/tests/std/numeric/bitShift.test.ts index ce87d830c6..11ab246b9d 100644 --- a/packages/typegpu/tests/std/numeric/bitShift.test.ts +++ b/packages/typegpu/tests/std/numeric/bitShift.test.ts @@ -1,7 +1,7 @@ -import { describe, expect, it, vi } from 'vitest'; -import { u32, i32, vec3f, vec3i, vec3u, f32, vec2u } from '../../../src/data/index.ts'; -import { bitShiftLeft, bitShiftRight } from '../../../src/std/index.ts'; -import tgpu from '../../../src/index.js'; +import { describe, expect, it } from 'vitest'; +import { u32, i32, vec3f, vec3i, vec3u, f32, vec2u } from 'typegpu/data'; +import { bitShiftLeft, bitShiftRight } from 'typegpu/std'; +import tgpu from 'typegpu'; describe('bit shift', () => { it('casts rhs to u32', () => { diff --git a/packages/typegpu/tests/std/numeric/cosh.test.ts b/packages/typegpu/tests/std/numeric/cosh.test.ts index 38a2aa6e1e..ea2fd141d1 100644 --- a/packages/typegpu/tests/std/numeric/cosh.test.ts +++ b/packages/typegpu/tests/std/numeric/cosh.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { cosh, isCloseTo } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { cosh, isCloseTo } from 'typegpu/std'; describe('cosh', () => { it('computes cosh of a number', () => { diff --git a/packages/typegpu/tests/std/numeric/cross.test.ts b/packages/typegpu/tests/std/numeric/cross.test.ts index 0e469653e6..a1fa2d8ed0 100644 --- a/packages/typegpu/tests/std/numeric/cross.test.ts +++ b/packages/typegpu/tests/std/numeric/cross.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec3f, vec3h } from '../../../src/data/index.ts'; -import { cross } from '../../../src/std/index.ts'; +import { vec3f, vec3h } from 'typegpu/data'; +import { cross } from 'typegpu/std'; describe('cross', () => { it('computes cross product of two vec3f', () => { diff --git a/packages/typegpu/tests/std/numeric/distance.test.ts b/packages/typegpu/tests/std/numeric/distance.test.ts index b4a9be2c93..c20c4c1d41 100644 --- a/packages/typegpu/tests/std/numeric/distance.test.ts +++ b/packages/typegpu/tests/std/numeric/distance.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3h } from '../../../src/data/index.ts'; -import { distance } from '../../../src/std/index.ts'; +import { vec2f, vec3h } from 'typegpu/data'; +import { distance } from 'typegpu/std'; describe('distance', () => { it('computes distance between two points', () => { diff --git a/packages/typegpu/tests/std/numeric/div.test.ts b/packages/typegpu/tests/std/numeric/div.test.ts index 072f6037e7..1ac394e700 100644 --- a/packages/typegpu/tests/std/numeric/div.test.ts +++ b/packages/typegpu/tests/std/numeric/div.test.ts @@ -1,6 +1,6 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; -import tgpu, { d } from '../../../src/index.js'; -import { div, isCloseTo } from '../../../src/std/index.ts'; +import tgpu, { d } from 'typegpu'; +import { div, isCloseTo } from 'typegpu/std'; describe('div', () => { it('divides numbers just like js would', () => { diff --git a/packages/typegpu/tests/std/numeric/dot.test.ts b/packages/typegpu/tests/std/numeric/dot.test.ts index 0eedba51ab..0a646df32b 100644 --- a/packages/typegpu/tests/std/numeric/dot.test.ts +++ b/packages/typegpu/tests/std/numeric/dot.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f } from '../../../src/data/index.ts'; -import { dot } from '../../../src/std/index.ts'; +import { vec2f, vec3f } from 'typegpu/data'; +import { dot } from 'typegpu/std'; describe('dot', () => { it('computes dot product of two vec2f', () => { diff --git a/packages/typegpu/tests/std/numeric/exp2.test.ts b/packages/typegpu/tests/std/numeric/exp2.test.ts index d6f05134df..7c3f72f751 100644 --- a/packages/typegpu/tests/std/numeric/exp2.test.ts +++ b/packages/typegpu/tests/std/numeric/exp2.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec2h, vec3f, vec3h, vec4f, vec4h } from '../../../src/data/index.ts'; -import { isCloseTo } from '../../../src/std/index.ts'; -import { exp2 } from '../../../src/std/numeric.ts'; +import { vec2f, vec2h, vec3f, vec3h, vec4f, vec4h } from 'typegpu/data'; +import { isCloseTo, exp2 } from 'typegpu/std'; describe('exp2', () => { it('computes exp2 of a number', () => { diff --git a/packages/typegpu/tests/std/numeric/fract.test.ts b/packages/typegpu/tests/std/numeric/fract.test.ts index 8284286b85..c90aeb8e2a 100644 --- a/packages/typegpu/tests/std/numeric/fract.test.ts +++ b/packages/typegpu/tests/std/numeric/fract.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { fract } from '../../../src/std/index.ts'; +import { fract } from 'typegpu/std'; describe('fract', () => { it('computes fractional part of a number', () => { diff --git a/packages/typegpu/tests/std/numeric/frexp.test.ts b/packages/typegpu/tests/std/numeric/frexp.test.ts index c35590d698..9949a83f7b 100644 --- a/packages/typegpu/tests/std/numeric/frexp.test.ts +++ b/packages/typegpu/tests/std/numeric/frexp.test.ts @@ -1,9 +1,8 @@ import { describe, expect, expectTypeOf } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import { frexp } from '../../../src/std/numeric.ts'; -import { vec2f, vec3h } from '../../../src/data/vector.ts'; -import type { v2f, v2i, v3i } from '../../../src/data/index.ts'; -import type { v3h } from '../../../src/data/wgslTypes.ts'; +import { frexp } from 'typegpu/std'; +import { vec2f, vec3h } from 'typegpu/data'; +import type { v2f, v2i, v3i, v3h } from 'typegpu/data'; describe('frexp', () => { it('gets inferred correctly', () => { diff --git a/packages/typegpu/tests/std/numeric/length.test.ts b/packages/typegpu/tests/std/numeric/length.test.ts index 6eed577943..6c5334ef9f 100644 --- a/packages/typegpu/tests/std/numeric/length.test.ts +++ b/packages/typegpu/tests/std/numeric/length.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { length } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { length } from 'typegpu/std'; describe('length', () => { it('computes length of vec2f', () => { diff --git a/packages/typegpu/tests/std/numeric/log.test.ts b/packages/typegpu/tests/std/numeric/log.test.ts index b39205f8ec..cf2eef6c82 100644 --- a/packages/typegpu/tests/std/numeric/log.test.ts +++ b/packages/typegpu/tests/std/numeric/log.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { isCloseTo, log } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { isCloseTo, log } from 'typegpu/std'; describe('log', () => { it('computes natural logarithm of numeric value', () => { diff --git a/packages/typegpu/tests/std/numeric/log2.test.ts b/packages/typegpu/tests/std/numeric/log2.test.ts index ffef6476d8..17cf617b2f 100644 --- a/packages/typegpu/tests/std/numeric/log2.test.ts +++ b/packages/typegpu/tests/std/numeric/log2.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { isCloseTo, log2 } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { isCloseTo, log2 } from 'typegpu/std'; describe('log2', () => { it('computes base-2 logarithm of numeric value', () => { diff --git a/packages/typegpu/tests/std/numeric/max.test.ts b/packages/typegpu/tests/std/numeric/max.test.ts index 40eee2dfd1..0a759ca837 100644 --- a/packages/typegpu/tests/std/numeric/max.test.ts +++ b/packages/typegpu/tests/std/numeric/max.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d, std } from '../../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('max', () => { it('acts as identity when called with one argument', () => { diff --git a/packages/typegpu/tests/std/numeric/min.test.ts b/packages/typegpu/tests/std/numeric/min.test.ts index ac3fa8da8a..3267c48284 100644 --- a/packages/typegpu/tests/std/numeric/min.test.ts +++ b/packages/typegpu/tests/std/numeric/min.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d, std } from '../../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('min', () => { it('acts as identity when called with one argument', () => { diff --git a/packages/typegpu/tests/std/numeric/mix.test.ts b/packages/typegpu/tests/std/numeric/mix.test.ts index 4e6891daab..28e6988165 100644 --- a/packages/typegpu/tests/std/numeric/mix.test.ts +++ b/packages/typegpu/tests/std/numeric/mix.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4h } from '../../../src/data/index.ts'; -import { mix } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4h } from 'typegpu/data'; +import { mix } from 'typegpu/std'; describe('mix', () => { it('should blend scalar values correctly', () => { diff --git a/packages/typegpu/tests/std/numeric/mod.test.ts b/packages/typegpu/tests/std/numeric/mod.test.ts index 06844ec878..125365b336 100644 --- a/packages/typegpu/tests/std/numeric/mod.test.ts +++ b/packages/typegpu/tests/std/numeric/mod.test.ts @@ -12,23 +12,10 @@ import { vec4h, vec4i, vec4u, -} from '../../../src/data/index.ts'; -import type { - v2f, - v2h, - v2i, - v2u, - v3f, - v3h, - v3i, - v3u, - v4f, - v4h, - v4i, - v4u, -} from '../../../src/data/wgslTypes.ts'; -import { isCloseTo, mod } from '../../../src/std/index.ts'; -import tgpu, { d } from '../../../src/index.js'; +} from 'typegpu/data'; +import type { v2f, v2h, v2i, v2u, v3f, v3h, v3i, v3u, v4f, v4h, v4i, v4u } from 'typegpu/data'; +import { isCloseTo, mod } from 'typegpu/std'; +import tgpu, { d } from 'typegpu'; describe('mod', () => { it('computes modulo of a number and a number', () => { diff --git a/packages/typegpu/tests/std/numeric/modf.test.ts b/packages/typegpu/tests/std/numeric/modf.test.ts index 3c1697bedc..3ce9ddca41 100644 --- a/packages/typegpu/tests/std/numeric/modf.test.ts +++ b/packages/typegpu/tests/std/numeric/modf.test.ts @@ -1,9 +1,8 @@ import { describe, expect, expectTypeOf } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import { modf } from '../../../src/std/numeric.ts'; -import { vec2f, vec3h } from '../../../src/data/vector.ts'; -import type { v2f } from '../../../src/data/index.ts'; -import type { v3h } from '../../../src/data/wgslTypes.ts'; +import { modf } from 'typegpu/std'; +import { vec2f, vec3h } from 'typegpu/data'; +import type { v2f, v3h } from 'typegpu/data'; describe('modf', () => { it('gets inferred correctly', () => { diff --git a/packages/typegpu/tests/std/numeric/mul.test.ts b/packages/typegpu/tests/std/numeric/mul.test.ts index 1b66472427..8d8e1bf02e 100644 --- a/packages/typegpu/tests/std/numeric/mul.test.ts +++ b/packages/typegpu/tests/std/numeric/mul.test.ts @@ -1,5 +1,5 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; -import tgpu, { d } from '../../../src/index.js'; +import tgpu, { d } from 'typegpu'; import { mat2x2f, mat3x3f, @@ -16,9 +16,9 @@ import { vec4h, vec4i, vec4u, -} from '../../../src/data/index.ts'; -import type { m2x2f, m3x3f, m4x4f, v2f, v3f, v4f } from '../../../src/data/wgslTypes.ts'; -import { mul } from '../../../src/std/index.ts'; +} from 'typegpu/data'; +import type { m2x2f, m3x3f, m4x4f, v2f, v3f, v4f } from 'typegpu/data'; +import { mul } from 'typegpu/std'; describe('mul', () => { it('computes product of a number and a number', () => { diff --git a/packages/typegpu/tests/std/numeric/normalize.test.ts b/packages/typegpu/tests/std/numeric/normalize.test.ts index f8e268b130..943270d5ac 100644 --- a/packages/typegpu/tests/std/numeric/normalize.test.ts +++ b/packages/typegpu/tests/std/numeric/normalize.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { normalize } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { normalize } from 'typegpu/std'; describe('normalize', () => { it('computes normalized vector from vec2f', () => { diff --git a/packages/typegpu/tests/std/numeric/pow.test.ts b/packages/typegpu/tests/std/numeric/pow.test.ts index 1c9739720d..2638220b68 100644 --- a/packages/typegpu/tests/std/numeric/pow.test.ts +++ b/packages/typegpu/tests/std/numeric/pow.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4h } from '../../../src/data/index.ts'; -import { pow } from '../../../src/std/index.ts'; +import { vec2f, vec3f, vec4h } from 'typegpu/data'; +import { pow } from 'typegpu/std'; describe('pow', () => { it('should return the correct power', () => { diff --git a/packages/typegpu/tests/std/numeric/reflect.test.ts b/packages/typegpu/tests/std/numeric/reflect.test.ts index f5cd209079..5bf54b22ae 100644 --- a/packages/typegpu/tests/std/numeric/reflect.test.ts +++ b/packages/typegpu/tests/std/numeric/reflect.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f } from '../../../src/data/index.ts'; -import { reflect } from '../../../src/std/index.ts'; +import { vec2f, vec3f } from 'typegpu/data'; +import { reflect } from 'typegpu/std'; describe('reflect', () => { it('reflects a vec2f vector correctly', () => { diff --git a/packages/typegpu/tests/std/numeric/round.test.ts b/packages/typegpu/tests/std/numeric/round.test.ts index 119f4762ec..2d9e3e9a9d 100644 --- a/packages/typegpu/tests/std/numeric/round.test.ts +++ b/packages/typegpu/tests/std/numeric/round.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d, std } from '../../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('round', () => { it('rounds to even numbers', () => { diff --git a/packages/typegpu/tests/std/numeric/sign.test.ts b/packages/typegpu/tests/std/numeric/sign.test.ts index 6ebc1ba0dd..00dc5857e6 100644 --- a/packages/typegpu/tests/std/numeric/sign.test.ts +++ b/packages/typegpu/tests/std/numeric/sign.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec3f } from '../../../src/data/index.ts'; -import { isCloseTo, sign } from '../../../src/std/index.ts'; +import { vec3f } from 'typegpu/data'; +import { isCloseTo, sign } from 'typegpu/std'; describe('sign', () => { it('computes sign of numeric value', () => { diff --git a/packages/typegpu/tests/std/numeric/smoothstep.test.ts b/packages/typegpu/tests/std/numeric/smoothstep.test.ts index 32fdc224b6..b821207d60 100644 --- a/packages/typegpu/tests/std/numeric/smoothstep.test.ts +++ b/packages/typegpu/tests/std/numeric/smoothstep.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec3f, vec4f } from '../../../src/data/index.ts'; -import { smoothstep } from '../../../src/std/index.ts'; -import { isCloseTo } from '../../../src/std/boolean.ts'; +import { vec2f, vec3f, vec4f } from 'typegpu/data'; +import { smoothstep, isCloseTo } from 'typegpu/std'; describe('smoothstep', () => { it('returns 0 when x is less than or equal to edge0', () => { diff --git a/packages/typegpu/tests/std/numeric/sub.test.ts b/packages/typegpu/tests/std/numeric/sub.test.ts index 99067479c6..ff62a7cfe0 100644 --- a/packages/typegpu/tests/std/numeric/sub.test.ts +++ b/packages/typegpu/tests/std/numeric/sub.test.ts @@ -1,5 +1,5 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; -import type { m3x3f, v2f, v3f, v4f } from '../../../src/data/index.ts'; +import type { m3x3f, v2f, v3f, v4f } from 'typegpu/data'; import { mat2x2f, mat3x3f, @@ -13,9 +13,8 @@ import { vec4f, vec4i, vec4u, -} from '../../../src/data/index.ts'; - -import { sub } from '../../../src/std/index.ts'; +} from 'typegpu/data'; +import { sub } from 'typegpu/std'; describe('sub', () => { it('computes difference of two vec2f', () => { diff --git a/packages/typegpu/tests/std/packing.test.ts b/packages/typegpu/tests/std/packing.test.ts index 816487a7fe..e7ed0e296d 100644 --- a/packages/typegpu/tests/std/packing.test.ts +++ b/packages/typegpu/tests/std/packing.test.ts @@ -1,11 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { vec2f, vec4f } from '../../src/data/vector.ts'; -import { - pack2x16float, - pack4x8unorm, - unpack2x16float, - unpack4x8unorm, -} from '../../src/std/packing.ts'; +import { vec2f, vec4f } from 'typegpu/data'; +import { pack2x16float, pack4x8unorm, unpack2x16float, unpack4x8unorm } from 'typegpu/std'; describe('packing', () => { it('packs and unpacks 4x8 unorm', () => { diff --git a/packages/typegpu/tests/std/range.test.ts b/packages/typegpu/tests/std/range.test.ts index d51e5971ed..aafbb1b511 100644 --- a/packages/typegpu/tests/std/range.test.ts +++ b/packages/typegpu/tests/std/range.test.ts @@ -1,7 +1,7 @@ import { test } from 'typegpu-testing-utility'; import { describe, expect } from 'vitest'; -import { d, std } from '../../src/index.js'; -import tgpu from '../../src/index.js'; +import { d, std } from 'typegpu'; +import tgpu from 'typegpu'; // range(n) — single argument, generates [0, n) test('std.range - single arg', () => { diff --git a/packages/typegpu/tests/std/texture/textureGather.test.ts b/packages/typegpu/tests/std/texture/textureGather.test.ts index 902cc8ae4e..243a755fcd 100644 --- a/packages/typegpu/tests/std/texture/textureGather.test.ts +++ b/packages/typegpu/tests/std/texture/textureGather.test.ts @@ -1,14 +1,11 @@ import { describe, expect, expectTypeOf } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import { textureGather } from '../../../src/std/texture.ts'; -import tgpu from '../../../src/index.js'; -import * as d from '../../../src/data/index.ts'; -import { bindGroupLayout } from '../../../src/tgpuBindGroupLayout.ts'; -import { resolve } from '../../../src/core/resolve/tgpuResolve.ts'; +import { textureGather } from 'typegpu/std'; +import tgpu, { d } from 'typegpu'; describe('textureGather', () => { it('Has correct signatures', () => { - const testLayout = bindGroupLayout({ + const testLayout = tgpu.bindGroupLayout({ tex2d: { texture: d.texture2d() }, tex2d_u32: { texture: d.texture2d(d.u32) }, tex2d_array: { texture: d.texture2dArray(d.i32) }, @@ -66,7 +63,7 @@ describe('textureGather', () => { } }); - expect(resolve([testFn])).toMatchInlineSnapshot(` + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` "@group(0) @binding(0) var tex2d: texture_2d; @group(0) @binding(6) var sampler_1: sampler; diff --git a/packages/typegpu/tests/std/texture/textureLoad.test.ts b/packages/typegpu/tests/std/texture/textureLoad.test.ts index ff498b7e05..a120b30788 100644 --- a/packages/typegpu/tests/std/texture/textureLoad.test.ts +++ b/packages/typegpu/tests/std/texture/textureLoad.test.ts @@ -1,17 +1,13 @@ import { describe, expect, expectTypeOf } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import { textureLoad } from '../../../src/std/texture.ts'; -import tgpu from '../../../src/index.js'; -import * as d from '../../../src/data/index.ts'; -import { bindGroupLayout } from '../../../src/tgpuBindGroupLayout.ts'; -import { resolve } from '../../../src/core/resolve/tgpuResolve.ts'; +import tgpu, { d, std } from 'typegpu'; // we need this since all other usages will be removed by plugin expectTypeOf(() => {}); describe('textureLoad', () => { it('Has correct signatures for sampled and depth textures', () => { - const testLayout = bindGroupLayout({ + const testLayout = tgpu.bindGroupLayout({ tex1d: { texture: d.texture1d() }, tex2d: { texture: d.texture2d() }, tex2d_u32: { texture: d.texture2d(d.u32) }, @@ -32,21 +28,21 @@ describe('textureLoad', () => { const arrayIndex = d.i32(0); const sampleIndex = d.i32(0); - const load1d = textureLoad(testLayout.$.tex1d, coord1d, level); - const load2d = textureLoad(testLayout.$.tex2d, coord2d, level); - const load2d_u32 = textureLoad(testLayout.$.tex2d_u32, coord2d, level); - const load2d_i32 = textureLoad(testLayout.$.tex2d_i32, coord2d, level); - const load2d_array = textureLoad(testLayout.$.tex2d_array, coord2d, arrayIndex, level); - const load3d = textureLoad(testLayout.$.tex3d, coord3d, level); - const loadms2d = textureLoad(testLayout.$.texms2d, coord2d, sampleIndex); - const loaddepth2d = textureLoad(testLayout.$.texdepth2d, coord2d, level); - const loaddepth2d_array = textureLoad( + const load1d = std.textureLoad(testLayout.$.tex1d, coord1d, level); + const load2d = std.textureLoad(testLayout.$.tex2d, coord2d, level); + const load2d_u32 = std.textureLoad(testLayout.$.tex2d_u32, coord2d, level); + const load2d_i32 = std.textureLoad(testLayout.$.tex2d_i32, coord2d, level); + const load2d_array = std.textureLoad(testLayout.$.tex2d_array, coord2d, arrayIndex, level); + const load3d = std.textureLoad(testLayout.$.tex3d, coord3d, level); + const loadms2d = std.textureLoad(testLayout.$.texms2d, coord2d, sampleIndex); + const loaddepth2d = std.textureLoad(testLayout.$.texdepth2d, coord2d, level); + const loaddepth2d_array = std.textureLoad( testLayout.$.texdepth2d_array, coord2d, arrayIndex, level, ); - const loaddepthms2d = textureLoad(testLayout.$.texdepthms2d, coord2d, sampleIndex); + const loaddepthms2d = std.textureLoad(testLayout.$.texdepthms2d, coord2d, sampleIndex); if (false) { expectTypeOf(load1d).toEqualTypeOf(); @@ -62,7 +58,7 @@ describe('textureLoad', () => { } }); - expect(resolve([testFn])).toMatchInlineSnapshot(` + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` "@group(0) @binding(0) var tex1d: texture_1d; @group(0) @binding(1) var tex2d: texture_2d; @@ -105,7 +101,7 @@ describe('textureLoad', () => { }); it('Has correct signatures for storage textures', () => { - const testLayout = bindGroupLayout({ + const testLayout = tgpu.bindGroupLayout({ store1d: { storageTexture: d.textureStorage1d('rgba32float', 'read-only') }, store2d: { storageTexture: d.textureStorage2d('rgba32float', 'read-only') }, store2d_uint: { storageTexture: d.textureStorage2d('rgba32uint', 'read-only') }, @@ -120,12 +116,12 @@ describe('textureLoad', () => { const coord3d = d.vec3i(0, 0, 0); const arrayIndex = d.i32(0); - const loadStore1d = textureLoad(testLayout.$.store1d, coord1d); - const loadStore2d = textureLoad(testLayout.$.store2d, coord2d); - const loadStore2d_uint = textureLoad(testLayout.$.store2d_uint, coord2d); - const loadStore2d_sint = textureLoad(testLayout.$.store2d_sint, coord2d); - const loadStore2d_array = textureLoad(testLayout.$.store2d_array, coord2d, arrayIndex); - const loadStore3d = textureLoad(testLayout.$.store3d, coord3d); + const loadStore1d = std.textureLoad(testLayout.$.store1d, coord1d); + const loadStore2d = std.textureLoad(testLayout.$.store2d, coord2d); + const loadStore2d_uint = std.textureLoad(testLayout.$.store2d_uint, coord2d); + const loadStore2d_sint = std.textureLoad(testLayout.$.store2d_sint, coord2d); + const loadStore2d_array = std.textureLoad(testLayout.$.store2d_array, coord2d, arrayIndex); + const loadStore3d = std.textureLoad(testLayout.$.store3d, coord3d); if (false) { expectTypeOf(loadStore1d).toEqualTypeOf(); @@ -137,7 +133,7 @@ describe('textureLoad', () => { } }); - expect(resolve([testFn])).toMatchInlineSnapshot(` + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` "@group(0) @binding(0) var store1d: texture_storage_1d; @group(0) @binding(1) var store2d: texture_storage_2d; @@ -166,21 +162,21 @@ describe('textureLoad', () => { }); it('Has correct signatures for external textures', () => { - const testLayout = bindGroupLayout({ + const testLayout = tgpu.bindGroupLayout({ texExternal: { externalTexture: d.textureExternal() }, }); const testFn = tgpu.fn([])(() => { const coord2d = d.vec2i(0, 0); - const loadExternal = textureLoad(testLayout.$.texExternal, coord2d); + const loadExternal = std.textureLoad(testLayout.$.texExternal, coord2d); if (false) { expectTypeOf(loadExternal).toEqualTypeOf(); } }); - expect(resolve([testFn])).toMatchInlineSnapshot(` + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` "@group(0) @binding(0) var texExternal: texture_external; fn testFn() { @@ -199,7 +195,7 @@ describe('textureLoad', () => { .$usage('sampled'); const sampledView = someTexture.createView(d.texture2d()); - const someLayout = bindGroupLayout({ + const someLayout = tgpu.bindGroupLayout({ tex2d: { texture: d.texture2d() }, }); @@ -207,13 +203,13 @@ describe('textureLoad', () => { const _validFn = tgpu.fn( [], d.vec4f, - )(() => textureLoad(sampledView.$, d.vec2i(0, 0), d.i32(0))); + )(() => std.textureLoad(sampledView.$, d.vec2i(0, 0), d.i32(0))); // Valid: view from a layout binding const _validFn2 = tgpu.fn( [], d.vec4f, - )(() => textureLoad(someLayout.$.tex2d, d.vec2i(0, 0), d.i32(0))); + )(() => std.textureLoad(someLayout.$.tex2d, d.vec2i(0, 0), d.i32(0))); // @ts-expect-error — raw schema must not be accepted const _invalidFn = tgpu.fn( @@ -221,7 +217,7 @@ describe('textureLoad', () => { d.vec4f, )(() => // @ts-expect-error - textureLoad(d.texture2d(), d.vec2i(0, 0), 0), + std.textureLoad(d.texture2d(), d.vec2i(0, 0), 0), ); }); }); diff --git a/packages/typegpu/tests/std/texture/textureSample.test.ts b/packages/typegpu/tests/std/texture/textureSample.test.ts index c1886cdb54..877fe739ec 100644 --- a/packages/typegpu/tests/std/texture/textureSample.test.ts +++ b/packages/typegpu/tests/std/texture/textureSample.test.ts @@ -1,9 +1,7 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import { textureSample } from '../../../src/std/texture.ts'; -import { fn } from '../../../src/core/function/tgpuFn.ts'; -import * as d from '../../../src/data/index.ts'; -import { bindGroupLayout } from '../../../src/tgpuBindGroupLayout.ts'; +import tgpu, { d } from 'typegpu'; +import { textureSample } from 'typegpu/std'; describe('textureSample', () => { it('does not allow for raw schemas to be passed in', ({ root }) => { @@ -20,21 +18,21 @@ describe('textureSample', () => { .$usage('sampled'); const sampledView = someTexture.createView(d.texture2d()); - const someLayout = bindGroupLayout({ + const someLayout = tgpu.bindGroupLayout({ sampledCube: { texture: d.textureCube() }, }); - const validFn = fn( + const validFn = tgpu.fn( [], d.vec4f, )(() => textureSample(sampledView.$, linSampler.$, d.vec2f(0.5))); - const validFn2 = fn( + const validFn2 = tgpu.fn( [], d.vec4f, )(() => textureSample(someLayout.$.sampledCube, linSampler.$, d.vec3f(0.5))); - const invalidFn = fn( + const invalidFn = tgpu.fn( [], d.vec4f, )(() => @@ -42,7 +40,7 @@ describe('textureSample', () => { textureSample(d.texture2d(), linSampler, d.vec2f(0.5)), ); - const invalidFn2 = fn( + const invalidFn2 = tgpu.fn( [], d.vec4f, )(() => diff --git a/packages/typegpu/tests/struct.test.ts b/packages/typegpu/tests/struct.test.ts index 14dc0984aa..306f210af1 100644 --- a/packages/typegpu/tests/struct.test.ts +++ b/packages/typegpu/tests/struct.test.ts @@ -18,7 +18,7 @@ import { vec3h, vec3u, } from '../src/data/index.ts'; -import tgpu from '../src/index.js'; +import tgpu from 'typegpu'; import * as d from '../src/data/index.ts'; import type { Infer } from '../src/shared/repr.ts'; import { frexp } from '../src/std/numeric.ts'; diff --git a/packages/typegpu/tests/swizzleMixedValidation.test.ts b/packages/typegpu/tests/swizzleMixedValidation.test.ts index 82e5069db0..a45d4a617c 100644 --- a/packages/typegpu/tests/swizzleMixedValidation.test.ts +++ b/packages/typegpu/tests/swizzleMixedValidation.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('Mixed swizzle validation', () => { describe('JS validation', () => { diff --git a/packages/typegpu/tests/texture.test.ts b/packages/typegpu/tests/texture.test.ts index 450537c8f5..886454ace2 100644 --- a/packages/typegpu/tests/texture.test.ts +++ b/packages/typegpu/tests/texture.test.ts @@ -5,7 +5,7 @@ import type { ExperimentalTgpuRoot } from '../src/core/root/rootTypes.ts'; import { it } from 'typegpu-testing-utility'; import * as d from '../src/data/index.ts'; import { attest } from '@ark/attest'; -import tgpu from '../src/index.js'; +import tgpu from 'typegpu'; import { getName } from '../src/shared/meta.ts'; describe('TgpuTexture', () => { diff --git a/packages/typegpu/tests/tgpuGenericFn.test.ts b/packages/typegpu/tests/tgpuGenericFn.test.ts index fd136ced01..0a11c756e7 100644 --- a/packages/typegpu/tests/tgpuGenericFn.test.ts +++ b/packages/typegpu/tests/tgpuGenericFn.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from 'vitest'; import * as d from '../src/data/index.ts'; -import tgpu, { std } from '../src/index.js'; +import tgpu, { std } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('TgpuGenericFn - shellless callback wrapper', () => { diff --git a/packages/typegpu/tests/tgsl/argumentOrigin.test.ts b/packages/typegpu/tests/tgsl/argumentOrigin.test.ts index 3fd4f3ce40..bfa935bc51 100644 --- a/packages/typegpu/tests/tgsl/argumentOrigin.test.ts +++ b/packages/typegpu/tests/tgsl/argumentOrigin.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('function argument origin tracking', () => { it('should fail on mutation of primitive arguments', () => { diff --git a/packages/typegpu/tests/tgsl/assignment.test.ts b/packages/typegpu/tests/tgsl/assignment.test.ts index dc8961413d..a84b39be8a 100644 --- a/packages/typegpu/tests/tgsl/assignment.test.ts +++ b/packages/typegpu/tests/tgsl/assignment.test.ts @@ -1,6 +1,6 @@ import { beforeEach, expect, type MockInstance, vi } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; let warnSpy: MockInstance; diff --git a/packages/typegpu/tests/tgsl/codeGen.test.ts b/packages/typegpu/tests/tgsl/codeGen.test.ts index 38f48adc6e..df7871f1f2 100644 --- a/packages/typegpu/tests/tgsl/codeGen.test.ts +++ b/packages/typegpu/tests/tgsl/codeGen.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('codeGen', () => { describe('vectors', () => { diff --git a/packages/typegpu/tests/tgsl/comptime.test.ts b/packages/typegpu/tests/tgsl/comptime.test.ts index f4fc57f42c..a4c65ae9c1 100644 --- a/packages/typegpu/tests/tgsl/comptime.test.ts +++ b/packages/typegpu/tests/tgsl/comptime.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('comptime', () => { it('should work in JS', () => { diff --git a/packages/typegpu/tests/tgsl/consoleLog.test.ts b/packages/typegpu/tests/tgsl/consoleLog.test.ts index c3fe5c7189..fe78a388da 100644 --- a/packages/typegpu/tests/tgsl/consoleLog.test.ts +++ b/packages/typegpu/tests/tgsl/consoleLog.test.ts @@ -1,18 +1,8 @@ -import { beforeEach, describe, expect, vi } from 'vitest'; -import { namespace } from '../../src/core/resolve/namespace.ts'; -import tgpu, { d } from '../../src/index.js'; -import { ResolutionCtxImpl } from '../../src/resolutionCtx.ts'; -import { deserializeAndStringify } from '../../src/tgsl/consoleLog/deserializers.ts'; -import { CodegenState } from '../../src/types.ts'; +import { describe, expect, vi } from 'vitest'; +import tgpu, { d } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('wgslGenerator with console.log', () => { - let ctx: ResolutionCtxImpl; - beforeEach(() => { - ctx = new ResolutionCtxImpl({ namespace: namespace() }); - ctx.pushMode(new CodegenState()); - }); - it('Parses console.log in a stray function to a comment and warns', () => { using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); @@ -683,131 +673,3 @@ describe('wgslGenerator with console.log', () => { expect(consoleWarnSpy).toHaveBeenCalledTimes(1); }); }); - -describe('deserializeAndStringify', () => { - it('works for string literals', () => { - const data = new Uint32Array([]); - const logInfo: (string | d.AnyWgslData)[] = ['String literal']; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "String literal", - ] - `, - ); - }); - - it('works for u32', () => { - const data = new Uint32Array([123]); - const logInfo: (string | d.AnyWgslData)[] = [d.u32]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "123", - ] - `, - ); - }); - - it('works for vec3u', () => { - const data = new Uint32Array([1, 2, 3]); - const logInfo: (string | d.AnyWgslData)[] = [d.vec3u]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "vec3u(1, 2, 3)", - ] - `, - ); - }); - - it('works for clumped vectors', () => { - const data = new Uint32Array([1, 2, 3, 4, 5, 6]); // no alignment - const logInfo: (string | d.AnyWgslData)[] = [d.vec3u, d.vec3u]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "vec3u(1, 2, 3)", - "vec3u(4, 5, 6)", - ] - `, - ); - }); - - it('works for multiple arguments', () => { - const data = new Uint32Array([1, 2, 3, 456]); - const logInfo: (string | d.AnyWgslData)[] = ['GID:', d.vec3u, 'Result:', d.u32]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "GID:", - "vec3u(1, 2, 3)", - "Result:", - "456", - ] - `, - ); - }); - - it('works for arrays', () => { - const data = new Uint32Array([1, 2, 3, 4]); - const logInfo: (string | d.AnyWgslData)[] = [d.arrayOf(d.u32, 4)]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "[1, 2, 3, 4]", - ] - `, - ); - }); - - it('works for nested arrays', () => { - const data = new Uint32Array([1, 2, 3, 4]); - const logInfo: (string | d.AnyWgslData)[] = [d.arrayOf(d.arrayOf(d.u32, 2), 2)]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "[[1, 2], [3, 4]]", - ] - `, - ); - }); - - it('works for structs', () => { - const data = new Uint32Array([1, 2, 3, 4]); - const logInfo: (string | d.AnyWgslData)[] = [d.struct({ vec: d.vec3u, num: d.u32 })]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "{ vec: vec3u(1, 2, 3), num: 4 }", - ] - `, - ); - }); - - it('works for nested structs', () => { - const data = new Uint32Array([1, 2, 3, 4, 1]); - const logInfo: (string | d.AnyWgslData)[] = [ - d.struct({ - nested: d.struct({ vec: d.vec3u, num: d.u32 }), - bool: d.bool, - }), - ]; - - expect(deserializeAndStringify(data, logInfo)).toMatchInlineSnapshot( - ` - [ - "{ nested: { vec: vec3u(1, 2, 3), num: 4 }, bool: true }", - ] - `, - ); - }); -}); diff --git a/packages/typegpu/tests/tgsl/entryFnParamPruning.test.ts b/packages/typegpu/tests/tgsl/entryFnParamPruning.test.ts index b60274fede..0b620c0a98 100644 --- a/packages/typegpu/tests/tgsl/entryFnParamPruning.test.ts +++ b/packages/typegpu/tests/tgsl/entryFnParamPruning.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d, std } from '../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; const layout = tgpu.bindGroupLayout({ output: { storage: d.arrayOf(d.f32), access: 'mutable' }, diff --git a/packages/typegpu/tests/tgsl/extensionEnabled.test.ts b/packages/typegpu/tests/tgsl/extensionEnabled.test.ts index 727e9fed70..dd1984f60d 100644 --- a/packages/typegpu/tests/tgsl/extensionEnabled.test.ts +++ b/packages/typegpu/tests/tgsl/extensionEnabled.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d, std } from '../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('extension based pruning', () => { it('should include extension code when the feature is used', () => { diff --git a/packages/typegpu/tests/tgsl/infixOperators.test.ts b/packages/typegpu/tests/tgsl/infixOperators.test.ts index 2e18ed3331..81c8322a86 100644 --- a/packages/typegpu/tests/tgsl/infixOperators.test.ts +++ b/packages/typegpu/tests/tgsl/infixOperators.test.ts @@ -1,5 +1,5 @@ import { describe, expect } from 'vitest'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('wgslGenerator', () => { diff --git a/packages/typegpu/tests/tgsl/memberAccess.test.ts b/packages/typegpu/tests/tgsl/memberAccess.test.ts index dfa33a9f34..bdef82c1da 100644 --- a/packages/typegpu/tests/tgsl/memberAccess.test.ts +++ b/packages/typegpu/tests/tgsl/memberAccess.test.ts @@ -1,8 +1,7 @@ import { describe } from 'vitest'; import { it } from 'typegpu-testing-utility'; import { expectSnippetOf } from '../utils/parseResolved.ts'; -import { snip } from '../../src/data/snippet.ts'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('Member Access', () => { const Boid = d.struct({ @@ -12,13 +11,13 @@ describe('Member Access', () => { it('should access member properties of literals', () => { expectSnippetOf(() => { 'use gpu'; - Boid().pos; - }).toStrictEqual(snip('Boid().pos', d.vec3f, 'runtime')); + return Boid().pos; + }).toStrictEqual(['Boid().pos', d.vec3f, 'runtime']); expectSnippetOf(() => { 'use gpu'; - Boid().pos.xyz; - }).toStrictEqual(snip('Boid().pos.xyz', d.vec3f, 'runtime')); + return Boid().pos.xyz; + }).toStrictEqual(['Boid().pos.xyz', d.vec3f, 'runtime']); }); it('should access member properties of externals', () => { @@ -26,13 +25,13 @@ describe('Member Access', () => { expectSnippetOf(() => { 'use gpu'; - boid.pos; - }).toStrictEqual(snip(d.vec3f(1, 2, 3), d.vec3f, 'constant')); + return boid.pos; + }).toStrictEqual([d.vec3f(1, 2, 3), d.vec3f, 'constant']); expectSnippetOf(() => { 'use gpu'; - boid.pos.zyx; - }).toStrictEqual(snip(d.vec3f(3, 2, 1), d.vec3f, 'constant')); + return boid.pos.zyx; + }).toStrictEqual([d.vec3f(3, 2, 1), d.vec3f, 'constant']); }); it('should access member properties of variables', () => { @@ -40,13 +39,13 @@ describe('Member Access', () => { expectSnippetOf(() => { 'use gpu'; - boidVar.$.pos; - }).toStrictEqual(snip('boidVar.pos', d.vec3f, 'private')); + return boidVar.$.pos; + }).toStrictEqual(['boidVar.pos', d.vec3f, 'private']); expectSnippetOf(() => { 'use gpu'; - boidVar.$.pos.xyz; - }).toStrictEqual(snip('boidVar.pos.xyz', d.vec3f, 'runtime')); // < swizzles are new objects + return boidVar.$.pos.xyz; + }).toStrictEqual(['boidVar.pos.xyz', d.vec3f, 'runtime']); // < swizzles are new objects }); it('derefs access to local variables with proper address space', () => { @@ -56,8 +55,8 @@ describe('Member Access', () => { const boid = Boid(); // Taking a reference that is local to this function const boidRef = boid; - boidRef.pos; - }).toStrictEqual(snip('(*boidRef).pos', d.vec3f, 'this-function')); + return boidRef.pos; + }).toStrictEqual(['(*boidRef).pos', d.vec3f, 'this-function']); }); it('derefs access to storage with proper address space', ({ root }) => { @@ -68,14 +67,14 @@ describe('Member Access', () => { 'use gpu'; // Taking a reference to a storage variable const boidRef = boidReadonly.$; - boidRef.pos; - }).toStrictEqual(snip('(*boidRef).pos', d.vec3f, 'readonly')); + return boidRef.pos; + }).toStrictEqual(['(*boidRef).pos', d.vec3f, 'readonly']); expectSnippetOf(() => { 'use gpu'; // Taking a reference to a storage variable const boidRef = boidMutable.$; - boidRef.pos; - }).toStrictEqual(snip('(*boidRef).pos', d.vec3f, 'mutable')); + return boidRef.pos; + }).toStrictEqual(['(*boidRef).pos', d.vec3f, 'mutable']); }); }); diff --git a/packages/typegpu/tests/tgsl/nameClashes.test.ts b/packages/typegpu/tests/tgsl/nameClashes.test.ts index 86167245ed..054da46bab 100644 --- a/packages/typegpu/tests/tgsl/nameClashes.test.ts +++ b/packages/typegpu/tests/tgsl/nameClashes.test.ts @@ -1,5 +1,5 @@ import { expect } from 'vitest'; -import tgpu, { d, std } from '../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; import { test } from 'typegpu-testing-utility'; test('should differentiate parameter names from existing declarations', () => { diff --git a/packages/typegpu/tests/tgsl/operatorOverloads.test.ts b/packages/typegpu/tests/tgsl/operatorOverloads.test.ts index a351fefcb4..97a54000ee 100644 --- a/packages/typegpu/tests/tgsl/operatorOverloads.test.ts +++ b/packages/typegpu/tests/tgsl/operatorOverloads.test.ts @@ -1,5 +1,5 @@ import { describe, expect } from 'vitest'; -import { d, tgpu } from '../../src/index.js'; +import { d, tgpu } from 'typegpu'; import { test } from 'typegpu-testing-utility'; test('vec3f() +', () => { diff --git a/packages/typegpu/tests/tgsl/rawCodeSnippet.test.ts b/packages/typegpu/tests/tgsl/rawCodeSnippet.test.ts index b0dcd5f70b..f84c760de2 100644 --- a/packages/typegpu/tests/tgsl/rawCodeSnippet.test.ts +++ b/packages/typegpu/tests/tgsl/rawCodeSnippet.test.ts @@ -1,6 +1,6 @@ import { describe, expect, expectTypeOf } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d } from '../../src/index.js'; +import tgpu, { d } from 'typegpu'; describe('rawCodeSnippet', () => { it('should throw a descriptive error when called in JS', () => { diff --git a/packages/typegpu/tests/tgsl/shellless.test.ts b/packages/typegpu/tests/tgsl/shellless.test.ts index dffa03992f..85b705b3ba 100644 --- a/packages/typegpu/tests/tgsl/shellless.test.ts +++ b/packages/typegpu/tests/tgsl/shellless.test.ts @@ -7,7 +7,7 @@ import tgpu, { type TgpuSlot, type TgpuTextureView, type TgpuUniform, -} from '../../src/index.js'; +} from 'typegpu'; import { it } from 'typegpu-testing-utility'; describe('shellless', () => { diff --git a/packages/typegpu/tests/tgsl/ternaryOperator.test.ts b/packages/typegpu/tests/tgsl/ternaryOperator.test.ts index fd37903d12..80fb87ff32 100644 --- a/packages/typegpu/tests/tgsl/ternaryOperator.test.ts +++ b/packages/typegpu/tests/tgsl/ternaryOperator.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; -import tgpu, { d, std } from '../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('ternary operator', () => { it('should resolve to one of the branches', () => { diff --git a/packages/typegpu/tests/tgsl/typeInference.test.ts b/packages/typegpu/tests/tgsl/typeInference.test.ts index 10c6c81726..588bdd528b 100644 --- a/packages/typegpu/tests/tgsl/typeInference.test.ts +++ b/packages/typegpu/tests/tgsl/typeInference.test.ts @@ -1,5 +1,5 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; -import tgpu, { d, std } from '../../src/index.js'; +import tgpu, { d, std } from 'typegpu'; import { namespace } from '../../src/core/resolve/namespace.ts'; import { ResolutionCtxImpl } from '../../src/resolutionCtx.ts'; import { CodegenState } from '../../src/types.ts'; diff --git a/packages/typegpu/tests/tgsl/wgslGenerator.test.ts b/packages/typegpu/tests/tgsl/wgslGenerator.test.ts index 62c96fe428..93f2c90b64 100644 --- a/packages/typegpu/tests/tgsl/wgslGenerator.test.ts +++ b/packages/typegpu/tests/tgsl/wgslGenerator.test.ts @@ -6,7 +6,7 @@ import { abstractFloat, abstractInt } from '../../src/data/numeric.ts'; import { snip } from '../../src/data/snippet.ts'; import { Void, type WgslArray } from '../../src/data/wgslTypes.ts'; import { provideCtx } from '../../src/execMode.ts'; -import tgpu from '../../src/index.js'; +import tgpu from 'typegpu'; import { ResolutionCtxImpl } from '../../src/resolutionCtx.ts'; import { getMetaData } from '../../src/shared/meta.ts'; import { $internal } from '../../src/shared/symbols.ts'; @@ -16,7 +16,6 @@ import { CodegenState } from '../../src/types.ts'; import { it } from 'typegpu-testing-utility'; import { ArrayExpression } from '../../src/tgsl/generationHelpers.ts'; import { extractSnippetFromFn } from '../utils/parseResolved.ts'; -import { UnknownData } from '../../src/tgsl/shaderGenerator_members.ts'; const { NodeTypeCatalog: NODE } = tinyest; @@ -1086,7 +1085,7 @@ describe('wgslGenerator', () => { it('creates intermediate representation for array expression', () => { const testFn = () => { 'use gpu'; - [d.u32(1), 8, 8, 2]; + return [d.u32(1), 8, 8, 2]; }; const snippet = extractSnippetFromFn(testFn); diff --git a/packages/typegpu/tests/tgslFn.test.ts b/packages/typegpu/tests/tgslFn.test.ts index 529e9f0cfa..03b62868d7 100644 --- a/packages/typegpu/tests/tgslFn.test.ts +++ b/packages/typegpu/tests/tgslFn.test.ts @@ -1,7 +1,7 @@ import { attest } from '@ark/attest'; import { describe, expect } from 'vitest'; import { builtin } from '../src/builtin.ts'; -import tgpu, { d, type TgpuFn, type TgpuSlot } from '../src/index.js'; +import tgpu, { d, type TgpuFn, type TgpuSlot } from 'typegpu'; import { getName } from '../src/shared/meta.ts'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/unplugin/autoname.test.ts b/packages/typegpu/tests/unplugin/autoname.test.ts index 45faa7039b..513957bc3d 100644 --- a/packages/typegpu/tests/unplugin/autoname.test.ts +++ b/packages/typegpu/tests/unplugin/autoname.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from 'vitest'; import { struct } from '../../src/data/index.ts'; -import tgpu, { d, type TgpuBindGroupLayout } from '../../src/index.js'; +import tgpu, { d, type TgpuBindGroupLayout } from 'typegpu'; import { getName } from '../../src/shared/meta.ts'; import { it } from 'typegpu-testing-utility'; diff --git a/packages/typegpu/tests/unroll.test.ts b/packages/typegpu/tests/unroll.test.ts index c35689684e..02af9ec6f1 100644 --- a/packages/typegpu/tests/unroll.test.ts +++ b/packages/typegpu/tests/unroll.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from 'vitest'; import { it } from 'typegpu-testing-utility'; import * as d from '../src/data/index.ts'; -import tgpu, { std } from '../src/index.js'; +import tgpu, { std } from 'typegpu'; describe('tgpu.unroll', () => { it('called outside the gpu function returns passed iterable', () => { diff --git a/packages/typegpu/tests/utils/parseResolved.ts b/packages/typegpu/tests/utils/parseResolved.ts index b798ff9f8c..7576fedc31 100644 --- a/packages/typegpu/tests/utils/parseResolved.ts +++ b/packages/typegpu/tests/utils/parseResolved.ts @@ -1,69 +1,67 @@ import type * as tinyest from 'tinyest'; +import { NodeTypeCatalog as NODE } from 'tinyest'; import { type Assertion, expect } from 'vitest'; -import type { BaseData } from '../../src/data/index.ts'; -import type { UnknownData } from '../../src/data/dataTypes.ts'; -import { ResolutionCtxImpl } from '../../src/resolutionCtx.ts'; -import { provideCtx } from '../../src/execMode.ts'; -import { getMetaData } from '../../src/shared/meta.ts'; -import wgslGenerator from '../../src/tgsl/wgslGenerator.ts'; -import { namespace } from '../../src/core/resolve/namespace.ts'; -import type { Snippet } from '../../src/data/snippet.ts'; -import { $internal } from '../../src/shared/symbols.ts'; -import { CodegenState } from '../../src/types.ts'; +import tgpu, { d, ShaderGenerator, WgslGenerator } from 'typegpu'; -export function extractSnippetFromFn(cb: () => unknown): Snippet { - const ctx = new ResolutionCtxImpl({ - namespace: namespace({ names: 'strict' }), - }); +type Snippet = ShaderGenerator.Snippet; +type UnknownData = ShaderGenerator.UnknownData; +type Origin = ShaderGenerator.Origin; - return provideCtx(ctx, () => { - let pushedFnScope = false; - try { - const meta = getMetaData(cb); +class ExtractingGenerator extends WgslGenerator { + #fnDepth: number; - if (!meta || !meta.ast) { - throw new Error('No metadata found for the function'); - } + returnedSnippet: Snippet | undefined; - ctx.pushMode(new CodegenState()); - ctx[$internal].itemStateStack.pushItem(); - ctx[$internal].itemStateStack.pushFunctionScope( - 'normal', - [], - {}, - undefined, - (meta.externals as () => Record)() ?? {}, - ); - ctx.pushBlockScope(); - pushedFnScope = true; + constructor() { + super(); + this.#fnDepth = 0; + } - // Extracting the last expression from the block - const statements = meta.ast.body[1] ?? []; - if (statements.length === 0) { - throw new Error(`Expected at least one expression, got ${statements.length}`); - } + public functionDefinition(body: tinyest.Block): string { + this.#fnDepth++; + try { + return super.functionDefinition(body); + } finally { + this.#fnDepth--; + } + } - wgslGenerator.initGenerator(ctx); - // Prewarming statements - for (const statement of statements) { - wgslGenerator._statement(statement); + public _return(statement: tinyest.Return): string { + if (this.#fnDepth === 1) { + if (this.returnedSnippet) { + throw new Error('Cannot inspect multiple return values'); } - return wgslGenerator._expression(statements[statements.length - 1] as tinyest.Expression); - } finally { - if (pushedFnScope) { - ctx.popBlockScope(); - ctx[$internal].itemStateStack.pop('functionScope'); - ctx[$internal].itemStateStack.pop('item'); + if (!statement[1]) { + throw new Error('Cannot inspect if nothing is returned'); } - ctx.popMode('codegen'); + this.returnedSnippet = this._expression(statement[1]); + return super._return([NODE.return]); } - }); + + // Proceed as usual + return super._return(statement); + } +} + +export function extractSnippetFromFn(cb: () => unknown): Snippet { + const generator = new ExtractingGenerator(); + + tgpu.resolve([cb], { unstable_shaderGenerator: generator }); + + if (!generator.returnedSnippet) { + throw new Error('Something must be returned to be inspected'); + } + + return generator.returnedSnippet; } -export function expectSnippetOf(cb: () => unknown): Assertion { - return expect(extractSnippetFromFn(cb)); +export function expectSnippetOf( + cb: () => unknown, +): Assertion<[unknown, d.BaseData | UnknownData, Origin]> { + const snippet = extractSnippetFromFn(cb); + return expect([snippet.value, snippet.dataType, snippet.origin]); } -export function expectDataTypeOf(cb: () => unknown): Assertion { - return expect(extractSnippetFromFn(cb).dataType); +export function expectDataTypeOf(cb: () => unknown): Assertion { + return expect(extractSnippetFromFn(cb).dataType); } diff --git a/packages/typegpu/tests/variable.test.ts b/packages/typegpu/tests/variable.test.ts index fc425ab9ef..dc98bf5ac3 100644 --- a/packages/typegpu/tests/variable.test.ts +++ b/packages/typegpu/tests/variable.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import type { TgpuVar, VariableScope } from '../src/core/variable/tgpuVariable.ts'; -import tgpu, { d, std } from '../src/index.js'; +import tgpu, { d, std } from 'typegpu'; describe('tgpu.privateVar|tgpu.workgroupVar', () => { it('should inject variable declaration when used in functions', () => { diff --git a/packages/typegpu/tests/vector.test.ts b/packages/typegpu/tests/vector.test.ts index 87149c62a0..f157574fce 100644 --- a/packages/typegpu/tests/vector.test.ts +++ b/packages/typegpu/tests/vector.test.ts @@ -2,7 +2,7 @@ import { BufferReader, BufferWriter } from 'typed-binary'; import { describe, expect, expectTypeOf, it } from 'vitest'; import { readData, writeData } from '../src/data/dataIO.ts'; import { sizeOf } from '../src/data/sizeOf.ts'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import * as std from '../src/std/index.ts'; describe('constructors', () => { diff --git a/packages/typegpu/tests/vertexLayout.test.ts b/packages/typegpu/tests/vertexLayout.test.ts index b107968e88..6c15d1ba2d 100644 --- a/packages/typegpu/tests/vertexLayout.test.ts +++ b/packages/typegpu/tests/vertexLayout.test.ts @@ -1,7 +1,7 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; import { connectAttributesToShader } from '../src/core/vertexLayout/connectAttributesToShader.ts'; import type { ArrayToContainedAttribs } from '../src/core/vertexLayout/vertexAttribute.ts'; -import tgpu, { d } from '../src/index.js'; +import tgpu, { d } from 'typegpu'; import type { TgpuVertexAttrib } from '../src/shared/vertexFormat.ts'; describe('ArrayToContainedAttribs', () => { diff --git a/setupVitest.ts b/setupVitest.ts new file mode 100644 index 0000000000..84c23db803 --- /dev/null +++ b/setupVitest.ts @@ -0,0 +1,58 @@ +import type { TestProject } from 'vitest/node'; +import * as fs from 'node:fs/promises'; +import { existsSync, readdirSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; + +const PACKAGES_DIR = join(import.meta.dirname, 'packages'); + +interface PackageJson { + name: string; + main?: string | undefined; + types?: string | undefined; + exports?: Record | undefined; + publishConfig?: { + main?: string | undefined; + types?: string | undefined; + exports?: Record | undefined; + }; +} + +export default async (project: TestProject) => { + if (project.config.watch) { + // Skip testing built packages in watch mode + return; + } + + const packageJsonPaths = readdirSync(PACKAGES_DIR, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => join(PACKAGES_DIR, dirent.name, 'package.json')); + + const onExit: (() => void)[] = []; + + await Promise.all( + packageJsonPaths.map(async (path) => { + if (!existsSync(path)) return; + + const originalContent = await fs.readFile(path, 'utf-8'); + const packageJson: PackageJson = JSON.parse(originalContent); + + packageJson.main = packageJson.publishConfig?.main ?? packageJson.main; + packageJson.types = packageJson.publishConfig?.types ?? packageJson.types; + packageJson.exports = packageJson.publishConfig?.exports ?? packageJson.exports; + + await fs.writeFile(path, JSON.stringify(packageJson, null, 2), 'utf-8'); + + onExit.push(() => { + writeFileSync(path, originalContent, 'utf-8'); + }); + }), + ); + + process.on('SIGINT', () => { + onExit.forEach((cb) => cb()); + }); + + return () => { + onExit.forEach((cb) => cb()); + }; +}; diff --git a/vitest.config.mts b/vitest.config.mts index 2372abc9ac..0d90030057 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -2,6 +2,7 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { + globalSetup: ['./setupVitest.ts'], projects: ['packages/*', 'apps/*'], }, });