Skip to content

Commit

Permalink
refactor(vector-pools): update attrib type handling
Browse files Browse the repository at this point in the history
BREAKING CHANGE: update attrib types to use string consts

- part of umbrella-wide changes to thi.ng/api Type aliases
  (see a333d41)
- remove obsolete asNativeType()/asGLType() fns
  (moved to thi.ng/api for better re-use)
  • Loading branch information
postspectacular committed Feb 2, 2021
1 parent c52632f commit 0ebd889
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 48 deletions.
24 changes: 13 additions & 11 deletions packages/vector-pools/src/attrib-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import {
assert,
IObjectOf,
IRelease,
SIZEOF,
sizeOf,
TypedArray,
typedArray,
TYPEDARRAY_CTORS,
} from "@thi.ng/api";
import { align, Pow2 } from "@thi.ng/binary";
import { isNumber } from "@thi.ng/checks";
import { MemPool } from "@thi.ng/malloc";
import { range } from "@thi.ng/transducers";
import { ReadonlyVec, Vec, zeroes } from "@thi.ng/vectors";
import { AttribPoolOpts, AttribSpec, LOGGER } from "./api";
import { asNativeType } from "./convert";

/*
* 0x00 0x08 0x10 0x18
Expand Down Expand Up @@ -125,7 +123,7 @@ export class AttribPool implements IRelease {
const size = spec.size;
const stride = spec.stride!;
const src = this.attribs[id];
const dest = new TYPEDARRAY_CTORS[asNativeType(spec.type)](n * size);
const dest = typedArray(spec.type, n * size);
if (size > 1) {
for (let i = 0, j = 0; i < n; i++, j += stride) {
dest.set(src.subarray(j, j + size), i * size);
Expand Down Expand Up @@ -207,7 +205,7 @@ export class AttribPool implements IRelease {
for (let id in this.specs) {
const a = this.specs[id];
const buf = typedArray(
asNativeType(a.type),
a.type,
this.pool.buf,
newAddr + (a.byteOffset || 0),
(newCapacity - 1) * a.stride! + a.size
Expand All @@ -227,7 +225,7 @@ export class AttribPool implements IRelease {
let maxSize = inclExisting ? this.maxAttribSize : 1;
for (let id in specs) {
const a = specs[id];
const size = SIZEOF[asNativeType(a.type)];
const size = sizeOf(a.type);
maxSize = Math.max(maxSize, size);
maxStride = Math.max(maxStride, a.byteOffset + a.size * size);
}
Expand All @@ -242,7 +240,7 @@ export class AttribPool implements IRelease {
assert(!this.attribs[id], `attrib: ${id} already exists`);
const a = specs[id];
assert(a.size > 0, `attrib ${id}: illegal or missing size`);
const size = SIZEOF[asNativeType(a.type)];
const size = sizeOf(a.type);
a.default == null && (a.default = a.size > 1 ? zeroes(a.size) : 0);
const isNum = isNumber(a.default);
assert(
Expand Down Expand Up @@ -275,7 +273,7 @@ export class AttribPool implements IRelease {
for (let id in specs) {
const a = specs[id];
this.attribs[id] = typedArray(
asNativeType(a.type),
a.type,
this.pool.buf,
this.addr + (a.byteOffset || 0),
(this.capacity - 1) * a.stride! + a.size
Expand Down Expand Up @@ -363,10 +361,14 @@ const resizeAttribs = (
const newAttribs: IObjectOf<[TypedArray, number]> = {};
for (let id in specs) {
const a = specs[id];
const type = asNativeType(a.type);
const dStride = stride / SIZEOF[type];
const dStride = stride / sizeOf(a.type);
newAttribs[id] = [
typedArray(type, buf, dest + a.byteOffset, num * dStride + a.size),
typedArray(
a.type,
buf,
dest + a.byteOffset,
num * dStride + a.size
),
dStride,
];
}
Expand Down
23 changes: 0 additions & 23 deletions packages/vector-pools/src/convert.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/vector-pools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./api";
export * from "./alist";
export * from "./array-list";
export * from "./attrib-pool";
export * from "./convert";
export * from "./linked-list";
export * from "./vec-pool";
export * from "./wrap";
12 changes: 4 additions & 8 deletions packages/vector-pools/src/vec-pool.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { GLType, Type, TypedArray } from "@thi.ng/api";
import { asNativeType, GLType, Type, TypedArray } from "@thi.ng/api";
import { isTypedArray } from "@thi.ng/checks";
import { MemPool, MemPoolOpts, MemPoolStats } from "@thi.ng/malloc";
import type { StridedVec } from "@thi.ng/vectors";
import type { IVecPool } from "./api";
import { asNativeType } from "./convert";
import { wrap } from "./wrap";

export class VecPool implements IVecPool {
Expand All @@ -19,17 +18,14 @@ export class VecPool implements IVecPool {
return this.pool.stats();
}

malloc(
size: number,
type: GLType | Type = Type.F32
): TypedArray | undefined {
malloc(size: number, type: GLType | Type = "f32"): TypedArray | undefined {
return this.pool.callocAs(asNativeType(type), size);
}

mallocWrapped(
size: number,
stride = 1,
type: GLType | Type = Type.F32
type: GLType | Type = "f32"
): StridedVec | undefined {
const buf = this.pool.callocAs(asNativeType(type), size * stride);
return buf ? wrap(buf, size, 0, stride) : undefined;
Expand Down Expand Up @@ -64,7 +60,7 @@ export class VecPool implements IVecPool {
size: number,
cstride = 1,
estride = size,
type: GLType | Type = Type.F32
type: GLType | Type = "f32"
): StridedVec[] | undefined {
const buf = this.malloc(
Math.max(cstride, estride, size) * num,
Expand Down
9 changes: 4 additions & 5 deletions packages/vector-pools/test/attribs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Type } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
import * as assert from "assert";
import { AttribPool } from "../src/attrib-pool";
Expand All @@ -10,7 +9,7 @@ describe("vector-pools", () => {
num: 8,
attribs: {
pos: {
type: Type.F32,
type: "f32",
size: 2, // 8 bytes
byteOffset: 0,
data: [
Expand All @@ -19,20 +18,20 @@ describe("vector-pools", () => {
],
},
id: {
type: Type.U32,
type: "u32",
size: 1, // 4 bytes
byteOffset: 8,
data: [1, 2],
index: 4,
},
index: {
type: Type.U16,
type: "u16",
size: 1, // 2 bytes
byteOffset: 12,
data: [10, 20],
},
col: {
type: Type.U8,
type: "u8",
size: 4, // 4 bytes
byteOffset: 14,
data: [
Expand Down

0 comments on commit 0ebd889

Please sign in to comment.