3.1.128 • Published 2 days ago

@thi.ng/vector-pools v3.1.128

Weekly downloads
212
License
Apache-2.0
Repository
github
Last release
2 days ago

vector-pools

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Data structures for managing & working with strided, memory mapped vectors.

This still package provides several data structures for managing & working with memory mapped vectors. Together with @thi.ng/vectors, these structures enable high-level, zero-copy* manipulation of the underlying memory region and are largely intended for WebGL & WASM use cases, e.g. to provide JS friendly views of a structured data region of a WebGL or WASM memory buffer.

* The only copying taking place is to GPU memory

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

This package might be merged with and/or superseded by @thi.ng/ecs / @thi.ng/soa.

Related packages

  • @thi.ng/ecs - Entity Component System based around typed arrays & sparse sets
  • @thi.ng/malloc - ArrayBuffer based malloc() impl for hybrid JS/WASM use cases, based on thi.ng/tinyalloc
  • @thi.ng/soa - SOA & AOS memory mapped structured views with optional & extensible serialization
  • @thi.ng/unionstruct - C-style struct, union and bitfield read/write views of ArrayBuffers
  • @thi.ng/vectors - Optimized 2d/3d/4d and arbitrary length vector operations
  • @thi.ng/webgl - WebGL & GLSL abstraction layer

Installation

yarn add @thi.ng/vector-pools

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/vector-pools"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const vectorPools = await import("@thi.ng/vector-pools");

Package sizes (gzipped, pre-treeshake): ESM: 3.05 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
WebGL MSDF text rendering & particle systemDemoSource

API

Generated API docs

WebGL geometry definition / manipulation

import { AttribPool, GLType } from "@thi.ng/vector-pools";
import * as v from "@thi.ng/vectors";
import * as tx from "@thi.ng/transducers";

// create an interleaved (AOS layout) attribute buffer w/ default values
const geo = new AttribPool({
    // initial size in bytes (or provide ArrayBuffer or @thi.ng/malloc/MemPool)
    mem: { size: 0x200 },
    // num elements
    num: 4,
    // attrib specs (data mapping layout)
    attribs: {
        pos: { type: GLType.F32, size: 3, byteOffset: 0 },
        uv: { type: GLType.F32, size: 2, byteOffset: 12 },
        col: { type: GLType.F32, size: 3, default: [1, 1, 1], byteOffset: 20 },
        id: { type: GLType.U16, size: 1, byteOffset: 32 }
    }
});

// computed overall stride length
geo.byteStride
// 36

// set attrib values
geo.setAttribs({
    pos: { data: [[-5, 0, 0], [5, 0, 0], [5, 5, 0], [-5, 5, 0]]},
    uv: { data: [[0, 0], [1, 0], [1, 1], [0, 1]] }
});
// ...or individually
geo.setAttribValues("id", [0, 1, 2, 3]);

// get view of individual attrib val
geo.attribValue("pos", 3)
// Float32Array [ -5, 5, 0 ]

// zero-copy direct manipulation of mapped attrib val
v.mulN(null, geo.attribValue("pos", 3), 2);
// Float32Array [ -10, 10, 0 ]

// get iterator of mapped attrib vals (e.g. for batch processing)
[...geo.attribValues("pos")]
// [ Float32Array [ -5, 0, 0 ],
//   Float32Array [ 5, 0, 0 ],
//   Float32Array [ 5, 5, 0 ],
//   Float32Array [ -10, 10, 0 ] ]

// use with transducers, e.g. to map positions to colors
tx.run(
    tx.map(([pos, col]) => v.maddN(col, [0.5, 0.5, 0.5], v.normalize(col, pos), 0.5)),
    tx.zip(geo.attribValues("pos"), geo.attribValues("col"))
);

// updated colors
[...geo.attribValues("col")]
// [ Float32Array [ 0, 0.5, 0.5 ],
//   Float32Array [ 1, 0.5, 0.5 ],
//   Float32Array [ 0.8535534143447876, 0.8535534143447876, 0.5 ],
//   Float32Array [ 0.1464466154575348, 0.8535534143447876, 0.5 ] ]

// dynamically add another attrib
// this will change the overall stride length and re-align all existing attribs
geo.addAttribs({
    normal: { type: GLType.F32, size: 3, default: [0, 0, 1], byteOffset: 36 }
});

// updated overall stride length
geo.byteStride
// 48

// ...Webgl boilerplate omitted
const gl = ...

// only need to use & bind single (interleaved) buffer
// containing all attribs
buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, geo.bytes(), gl.STATIC_DRAW);

// helper fn to bind a single shader attrib
const initAttrib = (gl, loc, attrib) => {
    gl.enableVertexAttribArray(loc);
    gl.vertexAttribPointer(
        loc,
        attrib.size,
        attrib.type,
        false,
        attrib.byteStride, // computed by pool
        attrib.byteOffset
    );
};

initAttrib(gl, attribLocPosition, geo.specs.pos);
initAttrib(gl, attribLocNormal, geo.specs.normal);
initAttrib(gl, attribLocUV, geo.specs.uv);

WASM interop

// main.c

#include <emscripten.h>
#include <stdint.h>

typedef struct {
    float pos[3];
    float uv[2];
    float col[3];
    uint16_t id;
} Vertex;

Vertex vertices[] = {
    {.pos = {-5, 0, 0}, .uv = {0, 0}, .col = {1, 0, 0}, .id = 0},
    {.pos = {5, 0, 0}, .uv = {1, 0}, .col = {0, 1, 0}, .id = 1},
    {.pos = {5, 5, 0}, .uv = {1, 1}, .col = {0, 0, 1}, .id = 2},
    {.pos = {-5, 5, 0}, .uv = {0, 1}, .col = {1, 0, 1}, .id = 3},
};

int main() { return 0; }

EMSCRIPTEN_KEEPALIVE Vertex* getVertices() {
    return vertices;
}

EMSCRIPTEN_KEEPALIVE int getNumVertices() {
    return sizeof(vertices) / sizeof(Vertex);
}
import { Type } from "@thi.ng/api";

// ... WASM / Emscripten boilerplate omitted
const Module = ...

// initialize pool from mapped WASM memory
const geo = new vp.AttribPool(
    // map WASM memory
    Module.buffer,
    // num elements (obtained from C function)
    Module._getNumVertices(),
    // attrib specs (data mapping layout)
    // don't specify attrib defaults to avoid overriding
    // values already initialized by WASM code
    {
        pos: { type: Type.F32, size: 3, byteOffset: 0 },
        uv:  { type: Type.F32, size: 2, byteOffset: 12 },
        col: { type: Type.F32, size: 3, byteOffset: 20 },
        id:  { type: Type.U16, size: 1, byteOffset: 32 }
    },
    // pool options
    {
        // don't allow resizing (since we're mapping a fixed sized C array)
        resizable: false,
        // initialize mem pool to start @ C `vertices` array
        mempool: {
            start: Module._getVertices(),
        }
    }
);

[...geo.attribValues("pos")]
// [ Float32Array [ -5, 0, 0 ],
//   Float32Array [ 5, 0, 0 ],
//   Float32Array [ 5, 5, 0 ],
//   Float32Array [ -5, 5, 0 ] ]

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-vector-pools,
  title = "@thi.ng/vector-pools",
  author = "Karsten Schmidt",
  note = "https://thi.ng/vector-pools",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

3.1.128

2 days ago

3.1.127

5 days ago

3.1.126

8 days ago

3.1.125

17 days ago

3.1.124

20 days ago

3.1.123

30 days ago

3.1.122

1 month ago

3.1.121

1 month ago

3.1.120

1 month ago

3.1.119

1 month ago

3.1.118

1 month ago

3.1.117

1 month ago

3.1.116

2 months ago

3.1.115

2 months ago

3.1.114

2 months ago

3.1.113

2 months ago

3.1.112

2 months ago

3.1.111

2 months ago

3.1.110

2 months ago

3.1.109

2 months ago

3.1.108

2 months ago

3.1.107

2 months ago

3.1.106

2 months ago

3.1.105

2 months ago

3.1.104

3 months ago

3.1.103

3 months ago

3.1.99

3 months ago

3.1.102

3 months ago

3.1.101

3 months ago

3.1.100

3 months ago

3.1.98

3 months ago

3.1.97

3 months ago

3.1.96

4 months ago

3.1.95

4 months ago

3.1.92

4 months ago

3.1.94

4 months ago

3.1.93

4 months ago

3.1.91

5 months ago

3.1.90

5 months ago

3.1.89

5 months ago

3.1.59

10 months ago

3.1.67

8 months ago

3.1.66

8 months ago

3.1.69

8 months ago

3.1.68

8 months ago

3.1.61

9 months ago

3.1.60

9 months ago

3.1.63

9 months ago

3.1.62

9 months ago

3.1.65

9 months ago

3.1.64

9 months ago

3.1.78

6 months ago

3.1.77

6 months ago

3.1.79

6 months ago

3.1.70

8 months ago

3.1.72

8 months ago

3.1.71

8 months ago

3.1.74

7 months ago

3.1.73

7 months ago

3.1.76

7 months ago

3.1.75

7 months ago

3.1.88

5 months ago

3.1.81

6 months ago

3.1.80

6 months ago

3.1.83

6 months ago

3.1.82

6 months ago

3.1.85

6 months ago

3.1.84

6 months ago

3.1.87

5 months ago

3.1.86

6 months ago

3.1.58

11 months ago

3.1.57

11 months ago

3.1.56

12 months ago

3.1.55

1 year ago

3.1.54

1 year ago

3.1.50

1 year ago

3.1.52

1 year ago

3.1.51

1 year ago

3.1.53

1 year ago

3.1.49

1 year ago

3.1.48

1 year ago

3.1.47

1 year ago

3.1.46

1 year ago

3.1.45

1 year ago

3.1.44

1 year ago

3.1.43

1 year ago

3.1.42

1 year ago

3.1.38

1 year ago

3.1.37

1 year ago

3.1.39

1 year ago

3.1.41

1 year ago

3.1.40

1 year ago

3.1.34

1 year ago

3.1.33

1 year ago

3.1.36

1 year ago

3.1.35

1 year ago

3.1.30

2 years ago

3.1.32

1 year ago

3.1.31

2 years ago

3.1.27

2 years ago

3.1.26

2 years ago

3.1.29

2 years ago

3.1.28

2 years ago

3.1.25

2 years ago

3.1.24

2 years ago

3.1.23

2 years ago

3.1.16

2 years ago

3.1.15

2 years ago

3.1.18

2 years ago

3.1.17

2 years ago

3.1.22

2 years ago

3.1.21

2 years ago

3.1.20

2 years ago

3.1.19

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.0.9

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.4

3 years ago

3.0.6

3 years ago

3.0.3

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.25

3 years ago

2.0.24

3 years ago

2.0.23

3 years ago

2.0.22

3 years ago

2.0.20

3 years ago

2.0.21

3 years ago

2.0.19

3 years ago

2.0.18

3 years ago

2.0.17

3 years ago

2.0.16

3 years ago

2.0.15

3 years ago

2.0.14

3 years ago

2.0.13

3 years ago

2.0.12

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.57

3 years ago

1.0.56

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.49

3 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.44

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago