Skip to content

Commit

Permalink
feat(soa): update SOAAttribSpec.buf to use ArrayBuffer w/ opt offset
Browse files Browse the repository at this point in the history
- update aos() buffer handling
- update SOA.initSpecs()
- update tests
  • Loading branch information
postspectacular committed Oct 18, 2019
1 parent 21c1ef6 commit 2759570
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
8 changes: 2 additions & 6 deletions packages/soa/src/aos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ export const aos = <K extends string>(
const spec = soaSpecs[id];
const tsize = SIZEOF[spec.type!];
spec.stride = total / tsize;
spec.buf = typedArray(
spec.type!,
buf,
byteOffset + offsets[id],
(num * total - offsets[id]) / tsize
);
spec.buf = buf;
spec.byteOffset = byteOffset + offsets[id];
}
return new SOA<K>(num, soaSpecs);
};
7 changes: 6 additions & 1 deletion packages/soa/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export interface SOAAttribSpec extends AOSAttribSpec {
/**
* Optional user supplied backing buffer.
*/
buf: TypedArray;
buf: ArrayBuffer;
/**
* Optional start offset for mapped region in backing buffer. Only
* used if `buf` is given.
*/
byteOffset: number;
/**
* Number of indices between each SOA value.
* MUST be >= size
Expand Down
9 changes: 6 additions & 3 deletions packages/soa/src/soa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export class SOA<K extends string> implements ILength {
const spec = prepareSpec(specs[id]);
this.validateSpec(id, spec);
const { stride, default: defVal } = spec;
const buffer = spec.buf || typedArray(spec.type!, num * stride!);
const buffer = spec.buf
? typedArray(spec.type!, spec.buf, spec.byteOffset || 0)
: typedArray(spec.type!, num * stride!);
if (defVal) {
for (let i = 0; i < num; i++) {
buffer.set(defVal, i * stride!);
Expand All @@ -180,8 +182,9 @@ export class SOA<K extends string> implements ILength {
assert(spec.stride! >= spec.size!, `${id} illegal stride`);
assert(
!spec.buf ||
spec.buf.length >=
(this.length - 1) * spec.stride! + spec.size!,
spec.buf.byteLength >=
((this.length - 1) * spec.stride! + spec.size!) *
SIZEOF[spec.type!],
`${id} buffer too small`
);
assert(
Expand Down
7 changes: 4 additions & 3 deletions packages/soa/test/aos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ describe("aos", () => {
struct.buffers.a.buffer === struct.buffers.b.buffer &&
struct.buffers.b.buffer === struct.buffers.c.buffer
);
assert.equal(struct.buffers.a.byteOffset, 0x100);
assert.equal(struct.buffers.b.byteOffset, 0x104);
assert.equal(struct.buffers.c.byteOffset, 0x10c);
assert.equal(struct.specs.a.stride, 8);
assert.equal(struct.specs.b.stride, 4);
assert.equal(struct.specs.c.stride, 16);
assert.equal(struct.buffers.a.byteOffset, 0x100);
assert.equal(struct.buffers.b.byteOffset, 0x104);
assert.equal(struct.buffers.c.byteOffset, 0x10c);
assert.equal(struct.buffers.a.buffer.byteLength, 0x100 + 0x20);
struct.setValues({
a: [[0x1001], [0x2002]],
b: [[1, 2], [3, 4]],
Expand Down

0 comments on commit 2759570

Please sign in to comment.