UNPKG

@thi.ng/shader-ast-js

Version:

Customizable JS codegen, compiler & runtime for @thi.ng/shader-ast

74 lines (73 loc) 1.92 kB
import { typedArray } from "@thi.ng/api"; import { outOfBounds } from "@thi.ng/errors/out-of-bounds"; import { set } from "@thi.ng/vectors/set"; import { setC2, setC3, setC4 } from "@thi.ng/vectors/setc"; import { setN, setN2, setN3, setN4 } from "@thi.ng/vectors/setn"; class Pool { constructor(type, size, cap) { this.size = size; this.cap = cap; this.mem = typedArray(type, cap * size); this.items = new Array(cap); for (let i = 0; i < cap; i++) { this.items[i] = this.mem.subarray(i * size, i * size + size); } const next = () => { if (this.index > this.items.length) outOfBounds(this.index); return this.items[this.index++]; }; let from; let uniform; switch (this.size) { case 2: from = (x, y) => setC2(next(), x, y); uniform = (n) => setN2(next(), n); case 3: from = (x, y, z) => setC3(next(), x, y, z); uniform = (n) => setN3(next(), n); case 4: from = (x, y, z, w) => setC4(next(), x, y, z, w); uniform = (n) => setN4(next(), n); default: from = (...args) => set(next(), args); uniform = (n) => setN(next(), n); } this.next = next; this.from = from; this.uniform = uniform; } mem; items; index = 0; next; from; uniform; reset() { this.index = 0; return this; } } const CAP = 2048; const POOL_VEC2 = new Pool("f32", 2, CAP); const POOL_VEC3 = new Pool("f32", 3, CAP); const POOL_VEC4 = new Pool("f32", 4, CAP); const POOL_IVEC2 = new Pool("i32", 2, CAP); const POOL_IVEC3 = new Pool("i32", 3, CAP); const POOL_IVEC4 = new Pool("i32", 4, CAP); const POOL_UVEC2 = new Pool("u32", 2, CAP); const POOL_UVEC3 = new Pool("u32", 3, CAP); const POOL_UVEC4 = new Pool("u32", 4, CAP); export { POOL_IVEC2, POOL_IVEC3, POOL_IVEC4, POOL_UVEC2, POOL_UVEC3, POOL_UVEC4, POOL_VEC2, POOL_VEC3, POOL_VEC4, Pool };