Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/typegpu/src/data/schemasEqual.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand All @@ -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) {
Expand Down
21 changes: 20 additions & 1 deletion packages/typegpu/tests/data/schemasEqualNominally.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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;
}"
`);
});
});
21 changes: 20 additions & 1 deletion packages/typegpu/tests/data/schemasEqualStructurally.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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;
}"
`);
});
});
Loading