diff --git a/packages/typegpu/src/data/schemasEqual.ts b/packages/typegpu/src/data/schemasEqual.ts index 6e48c3a322..3123550527 100644 --- a/packages/typegpu/src/data/schemasEqual.ts +++ b/packages/typegpu/src/data/schemasEqual.ts @@ -1,3 +1,4 @@ +import { comptime } from '../core/function/comptime.ts'; import type { AnyAttribute } from './attributes.ts'; import { isDisarray, isLooseDecorated, isUnstruct } from './dataTypes.ts'; import type { AnyData } from './dataTypes.ts'; @@ -19,9 +20,9 @@ import { isAtomic, isDecorated, isPtr, isWgslArray, isWgslStruct } from './wgslT * console.log(schemasEqualStructurally(struct({ a: vec3f }), struct({ a: vec3f }))); // true * console.log(schemasEqualStructurally(struct({ a: vec3f }), struct({ b: vec3f }))); // false */ -export function schemasEqualStructurally(a: AnyData, b: AnyData): boolean { - return schemasEqual(a, b, true); -} +export const schemasEqualStructurally = comptime((a: AnyData, b: AnyData): boolean => + schemasEqual(a, b, true), +).$name('schemasEqualStructurally'); /** * Performs a nominal comparison of two TypeGPU data schemas. This @@ -42,9 +43,9 @@ export function schemasEqualStructurally(a: AnyData, b: AnyData): boolean { * console.log(schemasEqualNominally(d.arrayOf(struct1, 2), d.arrayOf(struct1, 2))); // true * console.log(schemasEqualNominally(struct1, struct2)); // false */ -export function schemasEqualNominally(a: AnyData, b: AnyData): boolean { - return schemasEqual(a, b, false); -} +export const schemasEqualNominally = comptime((a: AnyData, b: AnyData): boolean => + schemasEqual(a, b, false), +).$name('schemasEqualNominally'); function schemasEqual(a: AnyData, b: AnyData, structural: boolean): boolean { if (a === b) { diff --git a/packages/typegpu/tests/data/schemasEqualNominally.test.ts b/packages/typegpu/tests/data/schemasEqualNominally.test.ts index 07e919b31b..6bff35668d 100644 --- a/packages/typegpu/tests/data/schemasEqualNominally.test.ts +++ b/packages/typegpu/tests/data/schemasEqualNominally.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { d } from 'typegpu'; +import { tgpu, d } from 'typegpu'; describe('schemasEqualNominally', () => { it('compares simple types', () => { @@ -242,4 +242,23 @@ describe('schemasEqualNominally', () => { expect(d.schemasEqualNominally(d.arrayOf(d.f32, 4), d.disarrayOf(d.f32, 4))).toBe(false); expect(d.schemasEqualNominally(d.struct({ a: d.f32 }), d.f32)).toBe(false); }); + + it('can be called at comptime in a shader function', () => { + const A = d.struct({ a: d.f32 }); + const B = d.struct({ a: d.f32 }); + + const f = () => { + 'use gpu'; + if (d.schemasEqualNominally(A, B)) { + return 1; + } + return 0; + }; + + expect(tgpu.resolve([f])).toMatchInlineSnapshot(` + "fn f() -> i32 { + return 0; + }" + `); + }); }); diff --git a/packages/typegpu/tests/data/schemasEqualStructurally.test.ts b/packages/typegpu/tests/data/schemasEqualStructurally.test.ts index e73bad65e1..a065031e6d 100644 --- a/packages/typegpu/tests/data/schemasEqualStructurally.test.ts +++ b/packages/typegpu/tests/data/schemasEqualStructurally.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { d } from 'typegpu'; +import { tgpu, d } from 'typegpu'; describe('schemasEqualStructurally', () => { it('compares simple types', () => { @@ -225,4 +225,23 @@ describe('schemasEqualStructurally', () => { expect(d.schemasEqualStructurally(d.arrayOf(d.f32, 4), d.disarrayOf(d.f32, 4))).toBe(false); expect(d.schemasEqualStructurally(d.struct({ a: d.f32 }), d.f32)).toBe(false); }); + + it('can be called at comptime in a shader function', () => { + const A = d.struct({ a: d.f32 }); + const B = d.struct({ a: d.f32 }); + + const f = () => { + 'use gpu'; + if (d.schemasEqualStructurally(A, B)) { + return 1; + } + return 0; + }; + + expect(tgpu.resolve([f])).toMatchInlineSnapshot(` + "fn f() -> i32 { + return 1; + }" + `); + }); });