Skip to content

Commit 4627843

Browse files
authored
feat: Rename d.getOffsetInfoAt to d.memoryLayoutOf (#2205)
1 parent c2ce266 commit 4627843

5 files changed

Lines changed: 36 additions & 38 deletions

File tree

packages/typegpu/src/core/pipeline/computePipeline.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type { ExperimentalTgpuRoot } from '../root/rootTypes.ts';
3030
import type { TgpuSlot } from '../slot/slotTypes.ts';
3131
import { warnIfOverflow } from './limitsOverflow.ts';
3232
import {
33-
getOffsetInfoAt,
33+
memoryLayoutOf,
3434
type PrimitiveOffsetInfo,
3535
} from '../../data/offsetUtils.ts';
3636
import {
@@ -77,10 +77,10 @@ export interface TgpuComputePipeline
7777
/**
7878
* Dispatches compute workgroups using parameters read from a buffer.
7979
* The buffer must contain 3 consecutive u32 values (x, y, z workgroup counts).
80-
* To get the correct offset within complex data structures, use `d.getOffsetInfoAt(...)`.
80+
* To get the correct offset within complex data structures, use `d.memoryLayoutOf(...)`.
8181
*
8282
* @param indirectBuffer - Buffer marked with 'indirect' usage containing dispatch parameters
83-
* @param start - PrimitiveOffsetInfo pointing to the first dispatch parameter. If not provided, starts at offset 0. To obtain safe offsets, use `getOffsetInfoAt(...)`.
83+
* @param start - PrimitiveOffsetInfo pointing to the first dispatch parameter. If not provided, starts at offset 0. To obtain safe offsets, use `d.memoryLayoutOf(...)`.
8484
*/
8585
dispatchWorkgroupsIndirect<T extends AnyWgslData>(
8686
indirectBuffer: TgpuBuffer<T> & IndirectFlag,
@@ -244,14 +244,14 @@ class TgpuComputePipelineImpl implements TgpuComputePipeline {
244244
): void {
245245
const DISPATCH_SIZE = 12; // 3 x u32 (x, y, z)
246246

247-
let offsetInfo = start ?? getOffsetInfoAt(indirectBuffer.dataType);
247+
let offsetInfo = start ?? memoryLayoutOf(indirectBuffer.dataType);
248248

249249
if (typeof offsetInfo === 'number') {
250250
if (offsetInfo === 0) {
251-
offsetInfo = getOffsetInfoAt(indirectBuffer.dataType);
251+
offsetInfo = memoryLayoutOf(indirectBuffer.dataType);
252252
} else {
253253
console.warn(
254-
`dispatchWorkgroupsIndirect: Provided start offset ${offsetInfo} as a raw number. Use getOffsetInfoAt(...) to include contiguous padding info for safer validation.`,
254+
`dispatchWorkgroupsIndirect: Provided start offset ${offsetInfo} as a raw number. Use d.memoryLayoutOf(...) to include contiguous padding info for safer validation.`,
255255
);
256256
// When only an offset is provided, assume we have at least 12 bytes contiguous.
257257
offsetInfo = {

packages/typegpu/src/data/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export { PUBLIC_isContiguous as isContiguous } from './isContiguous.ts';
221221
export {
222222
PUBLIC_getLongestContiguousPrefix as getLongestContiguousPrefix,
223223
} from './getLongestContiguousPrefix.ts';
224-
export { getOffsetInfoAt } from './offsetUtils.ts';
224+
export { memoryLayoutOf } from './offsetUtils.ts';
225225
export { PUBLIC_alignmentOf as alignmentOf } from './alignmentOf.ts';
226226
export { builtin } from '../builtin.ts';
227227
export { deepEqual } from './deepEqual.ts';

packages/typegpu/src/data/offsetUtils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,23 @@ export interface PrimitiveOffsetInfo {
305305
}
306306

307307
/**
308-
* A function that retrieves offset information for a specific primitive within a data schema.
308+
* A function that retrieves offset and information for a specific primitive within a data schema.
309309
* Example usage:
310310
* ```ts
311311
* const Boid = d.struct({
312312
* position: d.vec3f,
313313
* velocity: d.vec3f,
314314
* });
315-
* const offsetInfo = d.getOffsetInfoAt(Boid, (b) => b.velocity.y);
316-
* console.log(offsetInfo.offset); // Byte offset of velocity.y within Boid (here 20 bytes)
317-
* console.log(offsetInfo.contiguous); // Contiguous bytes available from that offset (here 8 bytes)
315+
* const memLayout = d.memoryLayoutOf(Boid, (b) => b.velocity.y);
316+
* console.log(memLayout.offset); // Byte offset of velocity.y within Boid (here 20 bytes)
317+
* console.log(memLayout.contiguous); // Contiguous bytes available from that offset (here 8 bytes)
318318
* ```
319319
*
320320
* @param schema - The data schema to analyze.
321321
* @param accessor - Optional function that accesses a specific primitive within the schema. If omitted, uses the root offset (0).
322322
* @returns An object containing the offset and contiguous byte information.
323323
*/
324-
export function getOffsetInfoAt<T extends BaseData>(
324+
export function memoryLayoutOf<T extends BaseData>(
325325
schema: T,
326326
accessor?: (proxy: Infer<T>) => number,
327327
): PrimitiveOffsetInfo {

packages/typegpu/tests/computePipeline.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import { $internal } from '../src/shared/symbols.ts';
1010
import { it } from './utils/extendedIt.ts';
1111
import { extensionEnabled } from '../src/std/extensions.ts';
12-
import { getOffsetInfoAt } from '../src/data/offsetUtils.ts';
1312

1413
describe('TgpuComputePipeline', () => {
1514
it('can be created with a compute entry function', ({ root, device }) => {
@@ -648,7 +647,7 @@ describe('TgpuComputePipeline', () => {
648647

649648
pipeline.dispatchWorkgroupsIndirect(
650649
buffer,
651-
getOffsetInfoAt(PaddedStruct, (s) => s.a),
650+
d.memoryLayoutOf(PaddedStruct, (s) => s.a),
652651
);
653652

654653
expect(warnSpy.mock.calls[0]![0]).toMatchInlineSnapshot(
@@ -658,7 +657,7 @@ describe('TgpuComputePipeline', () => {
658657
const deepBuffer = root.createBuffer(DeepStruct).$usage('indirect');
659658
pipeline.dispatchWorkgroupsIndirect(
660659
deepBuffer,
661-
getOffsetInfoAt(DeepStruct, (s) => s.someData[11] as number),
660+
d.memoryLayoutOf(DeepStruct, (s) => s.someData[11] as number),
662661
);
663662

664663
expect(warnSpy.mock.calls[1]![0]).toMatchInlineSnapshot(
@@ -667,7 +666,7 @@ describe('TgpuComputePipeline', () => {
667666

668667
pipeline.dispatchWorkgroupsIndirect(
669668
deepBuffer,
670-
getOffsetInfoAt(
669+
d.memoryLayoutOf(
671670
DeepStruct,
672671
(s) => s.nested.innerNested[0]?.yy as number,
673672
),

packages/typegpu/tests/offsetUtils.test.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { describe, expect, it } from 'vitest';
22
import { d } from '../src/index.ts';
33
import { sizeOf } from '../src/data/sizeOf.ts';
4-
import { getOffsetInfoAt } from '../src/data/offsetUtils.ts';
54

6-
describe('getOffsetInfoAt (default)', () => {
5+
describe('d.memoryLayoutOf (default)', () => {
76
it('returns offset 0 and full contiguous size for a scalar', () => {
8-
const info = getOffsetInfoAt(d.u32);
7+
const info = d.memoryLayoutOf(d.u32);
98

109
expect(info.offset).toBe(0);
1110
expect(info.contiguous).toBe(sizeOf(d.u32));
@@ -17,34 +16,34 @@ describe('getOffsetInfoAt (default)', () => {
1716
b: d.vec3f,
1817
});
1918

20-
const info = getOffsetInfoAt(Schema);
19+
const info = d.memoryLayoutOf(Schema);
2120

2221
expect(info.offset).toBe(0);
2322
expect(info.contiguous).toBe(4);
2423
});
2524
});
2625

27-
describe('getOffsetInfoAt (vectors)', () => {
26+
describe('d.memoryLayoutOf (vectors)', () => {
2827
it('computes component offsets and remaining contiguous bytes', () => {
29-
const info = getOffsetInfoAt(d.vec4u, (v) => v.z);
28+
const info = d.memoryLayoutOf(d.vec4u, (v) => v.z);
3029

3130
expect(info.offset).toBe(8);
3231
expect(info.contiguous).toBe(8);
3332
});
3433

3534
it('supports numeric component access', () => {
36-
const info = getOffsetInfoAt(d.vec3f, (v) => v[1]);
35+
const info = d.memoryLayoutOf(d.vec3f, (v) => v[1]);
3736

3837
expect(info.offset).toBe(4);
3938
expect(info.contiguous).toBe(8);
4039
});
4140
});
4241

43-
describe('getOffsetInfoAt (arrays)', () => {
42+
describe('d.memoryLayoutOf (arrays)', () => {
4443
it('computes offsets for array elements without padding', () => {
4544
const Schema = d.arrayOf(d.u32, 6);
4645

47-
const info = getOffsetInfoAt(Schema, (a) => a[3] as number);
46+
const info = d.memoryLayoutOf(Schema, (a) => a[3] as number);
4847

4948
expect(info.offset).toBe(12);
5049
expect(info.contiguous).toBe(12);
@@ -53,22 +52,22 @@ describe('getOffsetInfoAt (arrays)', () => {
5352
it('limits contiguous bytes to element size when array stride has padding', () => {
5453
const Schema = d.arrayOf(d.vec3u, 3);
5554

56-
const info = getOffsetInfoAt(Schema, (a) => a[1]?.x as number);
55+
const info = d.memoryLayoutOf(Schema, (a) => a[1]?.x as number);
5756

5857
expect(info.offset).toBe(16);
5958
expect(info.contiguous).toBe(12);
6059
});
6160
});
6261

63-
describe('getOffsetInfoAt (struct runs)', () => {
62+
describe('d.memoryLayoutOf (struct runs)', () => {
6463
it('returns contiguous bytes within a packed run', () => {
6564
const Schema = d.struct({
6665
a: d.u32,
6766
b: d.u32,
6867
c: d.u32,
6968
});
7069

71-
const info = getOffsetInfoAt(Schema, (s) => s.b);
70+
const info = d.memoryLayoutOf(Schema, (s) => s.b);
7271

7372
expect(info.offset).toBe(4);
7473
expect(info.contiguous).toBe(8);
@@ -80,14 +79,14 @@ describe('getOffsetInfoAt (struct runs)', () => {
8079
b: d.vec3u,
8180
});
8281

83-
const info = getOffsetInfoAt(Schema, (s) => s.a);
82+
const info = d.memoryLayoutOf(Schema, (s) => s.a);
8483

8584
expect(info.offset).toBe(0);
8685
expect(info.contiguous).toBe(4);
8786
});
8887
});
8988

90-
describe('getOffsetInfoAt (nested layouts)', () => {
89+
describe('d.memoryLayoutOf (nested layouts)', () => {
9190
// offset calculator for this struct: https://shorturl.at/NQggS
9291
const DeepStruct = d.struct({
9392
someData: d.arrayOf(d.f32, 13),
@@ -110,7 +109,7 @@ describe('getOffsetInfoAt (nested layouts)', () => {
110109
});
111110

112111
it('tracks offsets and contiguous bytes within nested arrays', () => {
113-
const info = getOffsetInfoAt(
112+
const info = d.memoryLayoutOf(
114113
DeepStruct,
115114
(s) => s.someData[11] as number,
116115
);
@@ -120,7 +119,7 @@ describe('getOffsetInfoAt (nested layouts)', () => {
120119
});
121120

122121
it('tracks offsets for nested structs inside arrays', () => {
123-
const info = getOffsetInfoAt(
122+
const info = d.memoryLayoutOf(
124123
DeepStruct,
125124
(s) => s.nested.innerNested[1]?.myVec.x as number,
126125
);
@@ -130,7 +129,7 @@ describe('getOffsetInfoAt (nested layouts)', () => {
130129
});
131130

132131
it('tracks offsets inside a later struct run', () => {
133-
const info = getOffsetInfoAt(
132+
const info = d.memoryLayoutOf(
134133
DeepStruct,
135134
(s) => s.nested.additionalData[1] as number,
136135
);
@@ -140,7 +139,7 @@ describe('getOffsetInfoAt (nested layouts)', () => {
140139
});
141140
});
142141

143-
describe('getOffsetInfoAt (edge cases)', () => {
142+
describe('d.memoryLayoutOf (edge cases)', () => {
144143
it('tracks offsets between array elements', () => {
145144
const E = d.struct({
146145
x: d.u32,
@@ -151,7 +150,7 @@ describe('getOffsetInfoAt (edge cases)', () => {
151150
arr: d.arrayOf(E, 3),
152151
});
153152

154-
const info = getOffsetInfoAt(
153+
const info = d.memoryLayoutOf(
155154
S,
156155
(s) => s.arr[1]?.vec.x as number,
157156
);
@@ -170,7 +169,7 @@ describe('getOffsetInfoAt (edge cases)', () => {
170169
r: I,
171170
});
172171

173-
const info = getOffsetInfoAt(
172+
const info = d.memoryLayoutOf(
174173
S,
175174
(s) => s.l.vec.z,
176175
);
@@ -191,7 +190,7 @@ describe('getOffsetInfoAt (edge cases)', () => {
191190
arr: d.arrayOf(E, 4),
192191
});
193192

194-
const info = getOffsetInfoAt(
193+
const info = d.memoryLayoutOf(
195194
S,
196195
(s) => s.arr[1]?.x.x as number,
197196
);
@@ -210,7 +209,7 @@ describe('getOffsetInfoAt (edge cases)', () => {
210209
s: I,
211210
});
212211

213-
const info = getOffsetInfoAt(
212+
const info = d.memoryLayoutOf(
214213
S,
215214
(s) => s.arr[0]?.y as number,
216215
);

0 commit comments

Comments
 (0)