From 0ebd8893d3651df6c033d40ce59fd7e77a66f790 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Tue, 2 Feb 2021 10:01:09 +0000 Subject: [PATCH] refactor(vector-pools): update attrib type handling BREAKING CHANGE: update attrib types to use string consts - part of umbrella-wide changes to thi.ng/api Type aliases (see a333d4182) - remove obsolete asNativeType()/asGLType() fns (moved to thi.ng/api for better re-use) --- packages/vector-pools/src/attrib-pool.ts | 24 +++++++++++++----------- packages/vector-pools/src/convert.ts | 23 ----------------------- packages/vector-pools/src/index.ts | 1 - packages/vector-pools/src/vec-pool.ts | 12 ++++-------- packages/vector-pools/test/attribs.ts | 9 ++++----- 5 files changed, 21 insertions(+), 48 deletions(-) delete mode 100644 packages/vector-pools/src/convert.ts diff --git a/packages/vector-pools/src/attrib-pool.ts b/packages/vector-pools/src/attrib-pool.ts index a5406a5e8a..7b54fe106b 100644 --- a/packages/vector-pools/src/attrib-pool.ts +++ b/packages/vector-pools/src/attrib-pool.ts @@ -2,10 +2,9 @@ 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"; @@ -13,7 +12,6 @@ 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 @@ -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); @@ -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 @@ -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); } @@ -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( @@ -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 @@ -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, ]; } diff --git a/packages/vector-pools/src/convert.ts b/packages/vector-pools/src/convert.ts deleted file mode 100644 index 5a72a633a1..0000000000 --- a/packages/vector-pools/src/convert.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { GL2TYPE, GLType, Type, TYPE2GL } from "@thi.ng/api"; - -/** - * Returns canonical {@link @thi.ng/api#Type} value of `type` by first - * attempting to resolve it as {@link @thi.ng/api#GLType} enum. - * - * @example - * ```ts - * nativeType(GLType.F32) => Type.F32 - * nativeType(Type.F32) => Type.F32 - * ``` - * - * @param type - - */ -export const asNativeType = (type: GLType | Type): Type => { - const t = (GL2TYPE)[type]; - return t !== undefined ? t : type; -}; - -export const asGLType = (type: GLType | Type): GLType => { - const t = (TYPE2GL)[type]; - return t !== undefined ? t : type; -}; diff --git a/packages/vector-pools/src/index.ts b/packages/vector-pools/src/index.ts index 9d2d12613b..2a262e8c89 100644 --- a/packages/vector-pools/src/index.ts +++ b/packages/vector-pools/src/index.ts @@ -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"; diff --git a/packages/vector-pools/src/vec-pool.ts b/packages/vector-pools/src/vec-pool.ts index 1a96a1a762..86b569d3cb 100644 --- a/packages/vector-pools/src/vec-pool.ts +++ b/packages/vector-pools/src/vec-pool.ts @@ -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 { @@ -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; @@ -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, diff --git a/packages/vector-pools/test/attribs.ts b/packages/vector-pools/test/attribs.ts index 975de57993..9fa67981f8 100644 --- a/packages/vector-pools/test/attribs.ts +++ b/packages/vector-pools/test/attribs.ts @@ -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"; @@ -10,7 +9,7 @@ describe("vector-pools", () => { num: 8, attribs: { pos: { - type: Type.F32, + type: "f32", size: 2, // 8 bytes byteOffset: 0, data: [ @@ -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: [