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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/typegpu-react/src/core/helper-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useStableEvent from './use-stable-event.ts';
export function useStableSchema<T>(schema: T): [T, /* schemaChanged */ boolean] {
const prevSchemaRef = useRef(schema as d.AnyData);

if (!d.deepEqual(prevSchemaRef.current, schema as d.AnyData)) {
if (!d.schemasEqualStructurally(prevSchemaRef.current, schema as d.AnyData)) {
prevSchemaRef.current = schema as d.AnyData;
return [schema, true];
}
Expand Down
9 changes: 6 additions & 3 deletions packages/typegpu/src/core/slot/accessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type AnyData, isData, undecorate } from '../../data/dataTypes.ts';
import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts';
import { isSnippet, type Origin, snip, type Snippet } from '../../data/snippet.ts';
import { deepEqual } from '../../data/deepEqual.ts';
import { schemasEqualStructurally } from '../../data/schemasEqual.ts';
import { arrayOf } from '../../data/array.ts';
import { UnknownData } from '../../data/dataTypes.ts';
import { type AnyWgslData, type BaseData, isAbstract, isWgslArray } from '../../data/wgslTypes.ts';
Expand Down Expand Up @@ -103,9 +103,12 @@ function matchesSchema(accessorSchema: BaseData, providedType: BaseData | Unknow
const expected = undecorate(accessorSchema);
const provided = undecorate(providedType);
if (isWgslArray(expected) && expected.elementCount === 0 && isWgslArray(provided)) {
return deepEqual(expected.elementType as AnyData, provided.elementType as AnyData);
return schemasEqualStructurally(
Comment thread
iwoplaza marked this conversation as resolved.
expected.elementType as AnyData,
provided.elementType as AnyData,
);
}
return deepEqual(expected as AnyData, provided as AnyData);
return schemasEqualStructurally(expected as AnyData, provided as AnyData);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/typegpu/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ export { PUBLIC_getLongestContiguousPrefix as getLongestContiguousPrefix } from
export { memoryLayoutOf } from './offsetUtils.ts';
export { PUBLIC_alignmentOf as alignmentOf } from './alignmentOf.ts';
export { builtin } from '../builtin.ts';
export { deepEqual } from './deepEqual.ts';
export {
schemasEqualStructurally,
schemasEqualNominally,
/** @deprecated Use `schemasEqualStructurally` instead */ schemasEqualStructurally as deepEqual,
} from './schemasEqual.ts';
export type {
AnyBuiltin,
BuiltinClipDistances,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,48 @@ import { isWgslStorageTexture, isWgslTexture } from './texture.ts';
import { isAtomic, isDecorated, isPtr, isWgslArray, isWgslStruct } from './wgslTypes.ts';

/**
* Performs a deep comparison of two TypeGPU data schemas.
* Performs a structural comparison of two TypeGPU data schemas. This
* means two struct definitions are considered equal when they have the
* same properties. Use `schemasEqualNominally` for a nominal comparison.
*
* @param a The first data schema to compare.
* @param b The second data schema to compare.
* @returns `true` if the schemas are deeply equal, `false` otherwise.
* @returns `true` if the schemas are structurally equal, `false` otherwise.
*
* @example
* ```ts
* import { vec3f, struct, deepEqual } from 'typegpu/data';
* import { vec3f, struct, schemasEqualStructurally } from 'typegpu/data';
*
* const schema1 = struct({ a: vec3f });
* const schema2 = struct({ a: vec3f });
* const schema3 = struct({ b: vec3f });
* 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);
}

/**
* Performs a nominal comparison of two TypeGPU data schemas. This
* means two struct definitions are only considered equal when they
* have the same object identity. Use `schemasEqualStructurally` for
* a structural comparison.
*
* console.log(deepEqual(schema1, schema2)); // true
* console.log(deepEqual(schema1, schema3)); // false
* ```
* @param a The first data schema to compare.
* @param b The second data schema to compare.
* @returns `true` if the schemas are nominally equal, `false` otherwise.
*
* @example
* import { vec3f, struct, schemasEqualNominally } from 'typegpu/data';
*
* const struct1 = struct({ a: vec3f });
* const struct2 = struct({ a: vec3f });
*
* console.log(schemasEqualNominally(d.arrayOf(struct1, 2), d.arrayOf(struct1, 2))); // true
* console.log(schemasEqualNominally(struct1, struct2)); // false
*/
export function deepEqual(a: AnyData, b: AnyData): boolean {
export function schemasEqualNominally(a: AnyData, b: AnyData): boolean {
return schemasEqual(a, b, false);
}

function schemasEqual(a: AnyData, b: AnyData, structural: boolean): boolean {
if (a === b) {
return true;
}
Expand All @@ -33,6 +56,13 @@ export function deepEqual(a: AnyData, b: AnyData): boolean {
}

if ((isWgslStruct(a) && isWgslStruct(b)) || (isUnstruct(a) && isUnstruct(b))) {
if (!structural) {
// With nominal typing, structs are only considered equal when they
// generate the same declaration, i.e. have the same object identity.
// If they didn't pass the previous condition, then they're not equal
return false;
}

const aProps = a.propTypes;
const bProps = b.propTypes;
const aKeys = Object.keys(aProps);
Expand All @@ -49,7 +79,7 @@ export function deepEqual(a: AnyData, b: AnyData): boolean {
keyA !== keyB ||
!keyA ||
!keyB ||
!deepEqual(aProps[keyA] as AnyData, bProps[keyB] as AnyData)
!schemasEqual(aProps[keyA] as AnyData, bProps[keyB] as AnyData, structural)
) {
return false;
}
Expand All @@ -60,7 +90,7 @@ export function deepEqual(a: AnyData, b: AnyData): boolean {
if ((isWgslArray(a) && isWgslArray(b)) || (isDisarray(a) && isDisarray(b))) {
return (
a.elementCount === b.elementCount &&
deepEqual(a.elementType as AnyData, b.elementType as AnyData)
schemasEqual(a.elementType as AnyData, b.elementType as AnyData, structural)
);
}

Expand All @@ -69,17 +99,17 @@ export function deepEqual(a: AnyData, b: AnyData): boolean {
a.addressSpace === b.addressSpace &&
a.access === b.access &&
a.implicit === b.implicit &&
deepEqual(a.inner as AnyData, b.inner as AnyData)
schemasEqual(a.inner as AnyData, b.inner as AnyData, structural)
);
}

if (isAtomic(a) && isAtomic(b)) {
return deepEqual(a.inner, b.inner);
return schemasEqual(a.inner, b.inner, structural);
}

if (isWgslTexture(a) && isWgslTexture(b)) {
// Dimension and multisampling are already encoded in the type
return deepEqual(a.sampleType as AnyData, b.sampleType as AnyData);
return schemasEqual(a.sampleType as AnyData, b.sampleType as AnyData, structural);
}

if (isWgslStorageTexture(a) && isWgslStorageTexture(b)) {
Expand All @@ -88,7 +118,7 @@ export function deepEqual(a: AnyData, b: AnyData): boolean {
}

if ((isDecorated(a) && isDecorated(b)) || (isLooseDecorated(a) && isLooseDecorated(b))) {
if (!deepEqual(a.inner as AnyData, b.inner as AnyData)) {
if (!schemasEqual(a.inner as AnyData, b.inner as AnyData, structural)) {
return false;
}
if (a.attribs.length !== b.attribs.length) {
Expand Down
221 changes: 0 additions & 221 deletions packages/typegpu/tests/data/deepEqual.test.ts

This file was deleted.

Loading
Loading