Skip to content

Commit 2d5c003

Browse files
authored
Rename loose data to unstruct and disarray (#733)
1 parent 66093ec commit 2d5c003

File tree

17 files changed

+235
-268
lines changed

17 files changed

+235
-268
lines changed

packages/typegpu/src/core/vertexLayout/vertexAttribute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LooseArray, LooseStruct } from '../../data/dataTypes';
1+
import type { Disarray, Unstruct } from '../../data/dataTypes';
22
import type { WgslArray, WgslStruct } from '../../data/wgslTypes';
33
import type {
44
KindToAcceptedAttribMap,
@@ -15,7 +15,7 @@ import type {
1515
* - TgpuStruct<{ a: Vec3f, b: unorm8x2 }>
1616
* - TgpuStruct<{ nested: TgpuStruct<{ a: Vec3f }> }>
1717
*/
18-
export type DataToContainedAttribs<T> = T extends WgslStruct | LooseStruct
18+
export type DataToContainedAttribs<T> = T extends WgslStruct | Unstruct
1919
? {
2020
[Key in keyof T['propTypes']]: DataToContainedAttribs<
2121
T['propTypes'][Key]
@@ -30,7 +30,7 @@ export type DataToContainedAttribs<T> = T extends WgslStruct | LooseStruct
3030
/**
3131
* Interprets an array as a set of vertex attributes.
3232
*/
33-
export type ArrayToContainedAttribs<T extends WgslArray | LooseArray> =
33+
export type ArrayToContainedAttribs<T extends WgslArray | Disarray> =
3434
DataToContainedAttribs<T['elementType']>;
3535

3636
export type LayoutToAllowedAttribs<T> = T extends {

packages/typegpu/src/core/vertexLayout/vertexLayout.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { alignmentOf, customAlignmentOf } from '../../data/alignmentOf';
2-
import { isLooseDecorated, isLooseStruct } from '../../data/dataTypes';
3-
import type { LooseArray } from '../../data/dataTypes';
2+
import { isLooseDecorated, isUnstruct } from '../../data/dataTypes';
3+
import type { Disarray } from '../../data/dataTypes';
44
import { sizeOf } from '../../data/sizeOf';
55
import { isDecorated, isWgslStruct } from '../../data/wgslTypes';
66
import type { BaseWgslData, WgslArray } from '../../data/wgslTypes';
@@ -23,7 +23,7 @@ import type {
2323
// ----------
2424

2525
export interface TgpuVertexLayout<
26-
TData extends WgslArray | LooseArray = WgslArray | LooseArray,
26+
TData extends WgslArray | Disarray = WgslArray | Disarray,
2727
> extends TgpuNamable {
2828
readonly resourceType: 'vertex-layout';
2929
readonly label?: string | undefined;
@@ -37,7 +37,7 @@ export interface INTERNAL_TgpuVertexAttrib {
3737
readonly _layout: TgpuVertexLayout;
3838
}
3939

40-
export function vertexLayout<TData extends WgslArray | LooseArray>(
40+
export function vertexLayout<TData extends WgslArray | Disarray>(
4141
schemaForCount: (count: number) => TData,
4242
stepMode: 'vertex' | 'instance' = 'vertex',
4343
): TgpuVertexLayout<ExoticIO<TData>> {
@@ -58,7 +58,7 @@ export function isVertexLayout<T extends TgpuVertexLayout>(
5858
// --------------
5959

6060
function dataToContainedAttribs<
61-
TLayoutData extends WgslArray | LooseArray,
61+
TLayoutData extends WgslArray | Disarray,
6262
TData extends BaseWgslData,
6363
>(
6464
layout: TgpuVertexLayout<TLayoutData>,
@@ -89,7 +89,7 @@ function dataToContainedAttribs<
8989
) as DataToContainedAttribs<TData>;
9090
}
9191

92-
if (isLooseStruct(data)) {
92+
if (isUnstruct(data)) {
9393
let memberOffset = offset;
9494

9595
return Object.fromEntries(
@@ -132,7 +132,7 @@ function dataToContainedAttribs<
132132
throw new Error(`Unsupported data used in vertex layout: ${String(data)}`);
133133
}
134134

135-
class TgpuVertexLayoutImpl<TData extends WgslArray | LooseArray>
135+
class TgpuVertexLayoutImpl<TData extends WgslArray | Disarray>
136136
implements TgpuVertexLayout<TData>
137137
{
138138
public readonly resourceType = 'vertex-layout';

packages/typegpu/src/data/alignmentOf.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
type AnyData,
33
getCustomAlignment,
4-
isLooseArray,
4+
isDisarray,
55
isLooseDecorated,
6-
isLooseStruct,
6+
isUnstruct,
77
} from './dataTypes';
88
import { packedFormats } from './vertexFormatData';
99
import {
@@ -53,13 +53,13 @@ function computeAlignment(data: object): number {
5353
return alignmentOf(data.elementType);
5454
}
5555

56-
if (isLooseStruct(data)) {
56+
if (isUnstruct(data)) {
5757
// A loose struct is aligned to its first property.
5858
const firstProp = Object.values(data.propTypes)[0];
5959
return firstProp ? getCustomAlignment(firstProp) ?? 1 : 1;
6060
}
6161

62-
if (isLooseArray(data)) {
62+
if (isDisarray(data)) {
6363
return getCustomAlignment(data.elementType) ?? 1;
6464
}
6565

@@ -77,13 +77,13 @@ function computeAlignment(data: object): number {
7777
}
7878

7979
function computeCustomAlignment(data: BaseWgslData): number {
80-
if (isLooseStruct(data)) {
80+
if (isUnstruct(data)) {
8181
// A loose struct is aligned to its first property.
8282
const firstProp = Object.values(data.propTypes)[0];
8383
return firstProp ? customAlignmentOf(firstProp) : 1;
8484
}
8585

86-
if (isLooseArray(data)) {
86+
if (isDisarray(data)) {
8787
return customAlignmentOf(data.elementType);
8888
}
8989

packages/typegpu/src/data/dataIO.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import type { ISerialInput, ISerialOutput } from 'typed-binary';
22
import type { Infer, InferRecord } from '../shared/repr';
33
import alignIO from './alignIO';
44
import { alignmentOf, customAlignmentOf } from './alignmentOf';
5-
import type {
6-
AnyData,
7-
LooseArray,
8-
LooseDecorated,
9-
LooseStruct,
10-
} from './dataTypes';
5+
import type { AnyData, Disarray, LooseDecorated, Unstruct } from './dataTypes';
116
import { mat2x2f, mat3x3f, mat4x4f } from './matrix';
127
import { sizeOf } from './sizeOf';
138
import {
@@ -392,7 +387,7 @@ const dataWriters = {
392387
output.writeUint8(value.w * 255);
393388
},
394389

395-
'loose-array'(output, schema: LooseArray, value: unknown[]) {
390+
disarray(output, schema: Disarray, value: unknown[]) {
396391
const alignment = alignmentOf(schema);
397392

398393
alignIO(output, alignment);
@@ -409,7 +404,7 @@ const dataWriters = {
409404
output.seekTo(beginning + sizeOf(schema));
410405
},
411406

412-
'loose-struct'(output, schema: LooseStruct, value) {
407+
unstruct(output, schema: Unstruct, value) {
413408
for (const [key, property] of Object.entries(schema.propTypes)) {
414409
dataWriters[property.type]?.(output, property, value[key]);
415410
}
@@ -728,7 +723,7 @@ const dataReaders = {
728723
return vec4f(r, g, b, a);
729724
},
730725

731-
'loose-struct'(input, schema: LooseStruct) {
726+
unstruct(input, schema: Unstruct) {
732727
const result = {} as Record<string, unknown>;
733728

734729
for (const [key, property] of Object.entries(schema.propTypes)) {
@@ -738,7 +733,7 @@ const dataReaders = {
738733
return result as InferRecord<Record<string, wgsl.BaseWgslData>>;
739734
},
740735

741-
'loose-array'(input, schema: LooseArray) {
736+
disarray(input, schema: Disarray) {
742737
const alignment = alignmentOf(schema);
743738
const elements: unknown[] = [];
744739

packages/typegpu/src/data/dataTypes.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ import type { PackedData } from './vertexFormatData';
44
import * as wgsl from './wgslTypes';
55

66
/**
7-
* Array schema constructed via `d.looseArrayOf` function.
7+
* Array schema constructed via `d.disarrayOf` function.
88
*
99
* Useful for defining vertex buffers.
1010
* Elements in the schema are not aligned in respect to their `byteAlignment`,
1111
* unless they are explicitly decorated with the custom align attribute
1212
* via `d.align` function.
1313
*/
14-
export interface LooseArray<
14+
export interface Disarray<
1515
TElement extends wgsl.BaseWgslData = wgsl.BaseWgslData,
1616
> {
17-
readonly type: 'loose-array';
17+
readonly type: 'disarray';
1818
readonly elementCount: number;
1919
readonly elementType: TElement;
2020
readonly '~repr': Infer<TElement>[];
2121
}
2222

2323
/**
24-
* Struct schema constructed via `d.looseStruct` function.
24+
* Struct schema constructed via `d.unstruct` function.
2525
*
2626
* Useful for defining vertex buffers, as the standard layout restrictions do not apply.
2727
* Members are not aligned in respect to their `byteAlignment`,
2828
* unless they are explicitly decorated with the custom align attribute
2929
* via `d.align` function.
3030
*/
31-
export interface LooseStruct<
31+
export interface Unstruct<
3232
TProps extends Record<string, wgsl.BaseWgslData> = Record<
3333
string,
3434
wgsl.BaseWgslData
3535
>,
3636
> {
37-
readonly type: 'loose-struct';
37+
readonly type: 'unstruct';
3838
readonly propTypes: TProps;
3939
readonly '~repr': InferRecord<TProps>;
4040
}
@@ -50,60 +50,56 @@ export interface LooseDecorated<
5050
}
5151

5252
const looseTypeLiterals = [
53-
'loose-struct',
54-
'loose-array',
53+
'unstruct',
54+
'disarray',
5555
'loose-decorated',
5656
...vertexFormats,
5757
] as const;
5858

5959
export type LooseTypeLiteral = (typeof looseTypeLiterals)[number];
6060

61-
export type AnyLooseData =
62-
| LooseArray
63-
| LooseStruct
64-
| LooseDecorated
65-
| PackedData;
61+
export type AnyLooseData = Disarray | Unstruct | LooseDecorated | PackedData;
6662

6763
export function isLooseData(data: unknown): data is AnyLooseData {
6864
return looseTypeLiterals.includes((data as AnyLooseData)?.type);
6965
}
7066

7167
/**
72-
* Checks whether the passed in value is a loose-array schema,
68+
* Checks whether the passed in value is a disarray schema,
7369
* as opposed to, e.g., a regular array schema.
7470
*
7571
* Array schemas can be used to describe uniform and storage buffers,
76-
* whereas looseArray schemas cannot. Loose arrays are useful for
72+
* whereas disarray schemas cannot. Disarrays are useful for
7773
* defining vertex buffers instead.
7874
*
7975
* @example
80-
* isLooseArray(d.arrayOf(d.u32, 4)) // false
81-
* isLooseArray(d.looseArrayOf(d.u32, 4)) // true
82-
* isLooseArray(d.vec3f) // false
76+
* isDisarray(d.arrayOf(d.u32, 4)) // false
77+
* isDisarray(d.disarrayOf(d.u32, 4)) // true
78+
* isDisarray(d.vec3f) // false
8379
*/
84-
export function isLooseArray<T extends LooseArray>(
80+
export function isDisarray<T extends Disarray>(
8581
schema: T | unknown,
8682
): schema is T {
87-
return (schema as LooseArray)?.type === 'loose-array';
83+
return (schema as Disarray)?.type === 'disarray';
8884
}
8985

9086
/**
91-
* Checks whether passed in value is a looseStruct schema,
87+
* Checks whether passed in value is a unstruct schema,
9288
* as opposed to, e.g., a struct schema.
9389
*
9490
* Struct schemas can be used to describe uniform and storage buffers,
95-
* whereas looseStruct schemas cannot. Loose structs are useful for
91+
* whereas unstruct schemas cannot. Unstructs are useful for
9692
* defining vertex buffers instead.
9793
*
9894
* @example
99-
* isLooseStruct(d.struct({ a: d.u32 })) // false
100-
* isLooseStruct(d.looseStruct({ a: d.u32 })) // true
101-
* isLooseStruct(d.vec3f) // false
95+
* isUnstruct(d.struct({ a: d.u32 })) // false
96+
* isUnstruct(d.unstruct({ a: d.u32 })) // true
97+
* isUnstruct(d.vec3f) // false
10298
*/
103-
export function isLooseStruct<T extends LooseStruct>(
99+
export function isUnstruct<T extends Unstruct>(
104100
schema: T | unknown,
105101
): schema is T {
106-
return (schema as T)?.type === 'loose-struct';
102+
return (schema as T)?.type === 'unstruct';
107103
}
108104

109105
export function isLooseDecorated<T extends LooseDecorated>(
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Infer } from '../shared/repr';
2-
import type { AnyData, LooseArray } from './dataTypes';
2+
import type { AnyData, Disarray } from './dataTypes';
33
import type { Exotic } from './exotic';
44

55
// ----------
@@ -15,26 +15,26 @@ import type { Exotic } from './exotic';
1515
* via `d.align` function.
1616
*
1717
* @example
18-
* const looseArray = d.looseArrayOf(d.vec3f, 3); // packed array of vec3f
18+
* const disarray = d.disarrayOf(d.vec3f, 3); // packed array of vec3f
1919
*
2020
* @example
21-
* const looseArray = d.looseArrayOf(d.align(16, d.vec3f), 3);
21+
* const disarray = d.disarrayOf(d.align(16, d.vec3f), 3);
2222
*
2323
* @param elementType The type of elements in the array.
2424
* @param count The number of elements in the array.
2525
*/
26-
export const looseArrayOf = <TElement extends AnyData>(
26+
export const disarrayOf = <TElement extends AnyData>(
2727
elementType: TElement,
2828
count: number,
29-
): LooseArray<Exotic<TElement>> =>
30-
new LooseArrayImpl(elementType as Exotic<TElement>, count);
29+
): Disarray<Exotic<TElement>> =>
30+
new DisarrayImpl(elementType as Exotic<TElement>, count);
3131

3232
// --------------
3333
// Implementation
3434
// --------------
3535

36-
class LooseArrayImpl<TElement extends AnyData> implements LooseArray<TElement> {
37-
public readonly type = 'loose-array';
36+
class DisarrayImpl<TElement extends AnyData> implements Disarray<TElement> {
37+
public readonly type = 'disarray';
3838
/** Type-token, not available at runtime */
3939
public readonly '~repr'!: Infer<TElement>[];
4040

packages/typegpu/src/data/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export {
6666
arrayOf,
6767
} from './array';
6868
export type {
69-
LooseArray,
70-
LooseStruct,
69+
Disarray,
70+
Unstruct,
7171
LooseDecorated,
7272
AnyData,
7373
AnyLooseData,
@@ -86,8 +86,8 @@ export {
8686
vec4i,
8787
vec4u,
8888
} from './vector';
89-
export { looseArrayOf } from './looseArray';
90-
export { looseStruct } from './looseStruct';
89+
export { disarrayOf } from './disarray';
90+
export { unstruct } from './unstruct';
9191
export {
9292
mat2x2f,
9393
mat3x3f,
@@ -106,8 +106,8 @@ export {
106106
HasCustomLocation,
107107
} from './attributes';
108108
export {
109-
isLooseArray,
110-
isLooseStruct,
109+
isDisarray,
110+
isUnstruct,
111111
isLooseDecorated,
112112
isData,
113113
isLooseData,

0 commit comments

Comments
 (0)