Skip to content

Commit

Permalink
refactor(vector-pools): update AttribPool & VecPool ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 1, 2019
1 parent 6d15686 commit d7ccc88
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
12 changes: 7 additions & 5 deletions packages/vector-pools/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IRelease, TypedArray } from "@thi.ng/api";
import { Type, MemPoolOpts } from "@thi.ng/malloc";
import { StridedVec, Vec, ReadonlyVec } from "@thi.ng/vectors";
import { IObjectOf, IRelease, TypedArray } from "@thi.ng/api";
import { MemPool, MemPoolOpts, Type } from "@thi.ng/malloc";
import { ReadonlyVec, StridedVec, Vec } from "@thi.ng/vectors";

export interface AttribSpec {
type: GLType | Type;
Expand All @@ -11,8 +11,10 @@ export interface AttribSpec {
}

export interface AttribPoolOpts {
resizable: boolean;
mempool: MemPoolOpts;
mem: MemPool | Partial<MemPoolOpts>;
attribs?: IObjectOf<AttribSpec>;
num: number;
resizable?: boolean;
}

export interface IVecPool extends IRelease {
Expand Down
28 changes: 12 additions & 16 deletions packages/vector-pools/src/attrib-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,30 @@ export class AttribPool implements
attribs: IObjectOf<TypedArray>;
order: string[];
specs: IObjectOf<AttribSpec>;
opts: AttribPoolOpts;
pool: MemPool;
addr: number;
capacity: number;

byteStride: number;
maxAttribSize: number;
resizable: boolean;

constructor(
pool: number | ArrayBuffer | MemPool,
capacity: number,
specs: IObjectOf<AttribSpec>,
opts?: AttribPoolOpts
) {
this.opts = <AttribPoolOpts>{
constructor(opts?: AttribPoolOpts) {
opts = <AttribPoolOpts>{
resizable: true,
...opts
};
this.pool = !(pool instanceof MemPool) ?
new MemPool(pool, this.opts.mempool) :
pool;
this.capacity = capacity;
this.pool = !(opts.mem instanceof MemPool) ?
new MemPool(opts.mem) :
opts.mem;
this.capacity = opts.num;
this.resizable = opts.resizable;
this.specs = {};
this.attribs = {};
this.order = [];
this.byteStride = 1;
this.maxAttribSize = 1;
this.addAttribs(specs, true);
opts.attribs && this.addAttribs(opts.attribs, true);
}

bytes() {
Expand Down Expand Up @@ -183,7 +179,7 @@ export class AttribPool implements

ensure(newCapacity: number, fill = false) {
if (newCapacity <= this.capacity) return;
assert(this.opts.resizable, `pool resizing disabled`);
assert(this.resizable, `pool resizing disabled`);
const newAddr = this.pool.realloc(this.addr, newCapacity * this.byteStride);
assert(newAddr > 0, `out of memory`);
for (let id in this.specs) {
Expand Down Expand Up @@ -282,10 +278,10 @@ export class AttribPool implements
const grow = newByteStride > this.byteStride;
let newAddr = this.addr;
if (grow) {
assert(this.opts.resizable, `pool resizing disabled`);
assert(this.resizable, `pool resizing disabled`);
newAddr = this.pool.realloc(this.addr, this.capacity * newByteStride);
assert(newAddr > 0, `out of memory`);
} else if (!this.opts.resizable) {
} else if (!this.resizable) {
return;
}
const sameBlock = newAddr === this.addr;
Expand Down
12 changes: 6 additions & 6 deletions packages/vector-pools/src/vec-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export class VecPool implements

pool: MemPool;

constructor(pool: MemPool);
constructor(buf: number | ArrayBuffer, opts?: Partial<MemPoolOpts>);
constructor(pool: any, opts?: Partial<MemPoolOpts>) {
this.pool = !(pool instanceof MemPool) ?
new MemPool(pool, opts) :
pool;
constructor(pool?: MemPool);
constructor(opts?: Partial<MemPoolOpts>);
constructor(pool: any) {
this.pool = pool instanceof MemPool ?
pool :
new MemPool(pool);
}

stats(): MemPoolStats {
Expand Down
10 changes: 5 additions & 5 deletions packages/vector-pools/test/attribs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { Type } from "@thi.ng/malloc";

describe("vector-pools", () => {
it("attribs", () => {
const pool = new AttribPool(
0x100,
8,
{
const pool = new AttribPool({
mem: { size: 0x100 },
num: 8,
attribs: {
pos: { type: Type.F32, default: [0, 0], size: 2, byteOffset: 0 },
id: { type: Type.U32, default: 0, size: 1, byteOffset: 8 },
index: { type: Type.U16, default: 0, size: 1, byteOffset: 12 },
col: { type: Type.I8, default: [0, 0, 0, 0], size: 4, byteOffset: 14 },
}
);
});
pool.setAttribValue("pos", 0, [1, 2]);
pool.setAttribValue("id", 0, 1);
pool.setAttribValue("index", 0, 10);
Expand Down

0 comments on commit d7ccc88

Please sign in to comment.