3.2.29 • Published 15 days ago

@thi.ng/vector-pools v3.2.29

Weekly downloads
212
License
Apache-2.0
Repository
github
Last release
15 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.2.28

15 days ago

3.2.29

15 days ago

3.2.26

22 days ago

3.2.27

19 days ago

3.2.25

1 month ago

3.2.24

1 month ago

3.2.23

2 months ago

3.2.22

2 months ago

3.2.20

3 months ago

3.2.21

3 months ago

3.2.19

3 months ago

3.2.18

3 months ago

3.2.17

4 months ago

3.2.2

7 months ago

3.2.1

7 months ago

3.2.0

8 months ago

3.2.6

7 months ago

3.2.5

7 months ago

3.2.4

7 months ago

3.2.3

7 months ago

3.2.9

6 months ago

3.2.8

6 months ago

3.2.7

6 months ago

3.2.13

5 months ago

3.2.12

5 months ago

3.2.15

4 months ago

3.2.14

5 months ago

3.2.16

4 months ago

3.2.11

5 months ago

3.2.10

6 months ago

3.1.129

9 months ago

3.1.128

10 months ago

3.1.127

10 months ago

3.1.126

10 months ago

3.1.125

10 months ago

3.1.124

10 months ago

3.1.123

10 months ago

3.1.122

10 months ago

3.1.121

11 months ago

3.1.120

11 months ago

3.1.119

11 months ago

3.1.118

11 months ago

3.1.117

11 months ago

3.1.116

11 months ago

3.1.115

11 months ago

3.1.114

11 months ago

3.1.113

11 months ago

3.1.112

11 months ago

3.1.111

11 months ago

3.1.110

12 months ago

3.1.109

12 months ago

3.1.108

12 months ago

3.1.107

12 months ago

3.1.106

12 months ago

3.1.105

12 months ago

3.1.104

12 months ago

3.1.103

12 months ago

3.1.99

1 year ago

3.1.102

1 year ago

3.1.101

1 year ago

3.1.100

1 year ago

3.1.98

1 year ago

3.1.97

1 year ago

3.1.96

1 year ago

3.1.95

1 year ago

3.1.92

1 year ago

3.1.94

1 year ago

3.1.93

1 year ago

3.1.91

1 year ago

3.1.90

1 year ago

3.1.89

1 year ago

3.1.59

2 years ago

3.1.67

1 year ago

3.1.66

1 year ago

3.1.69

1 year ago

3.1.68

1 year ago

3.1.61

1 year ago

3.1.60

2 years ago

3.1.63

1 year ago

3.1.62

1 year ago

3.1.65

1 year ago

3.1.64

1 year ago

3.1.78

1 year ago

3.1.77

1 year ago

3.1.79

1 year ago

3.1.70

1 year ago

3.1.72

1 year ago

3.1.71

1 year ago

3.1.74

1 year ago

3.1.73

1 year ago

3.1.76

1 year ago

3.1.75

1 year ago

3.1.88

1 year ago

3.1.81

1 year ago

3.1.80

1 year ago

3.1.83

1 year ago

3.1.82

1 year ago

3.1.85

1 year ago

3.1.84

1 year ago

3.1.87

1 year ago

3.1.86

1 year ago

3.1.58

2 years ago

3.1.57

2 years ago

3.1.56

2 years ago

3.1.55

2 years ago

3.1.54

2 years ago

3.1.50

2 years ago

3.1.52

2 years ago

3.1.51

2 years ago

3.1.53

2 years ago

3.1.49

2 years ago

3.1.48

2 years ago

3.1.47

2 years ago

3.1.46

2 years ago

3.1.45

2 years ago

3.1.44

2 years ago

3.1.43

2 years ago

3.1.42

2 years ago

3.1.38

2 years ago

3.1.37

2 years ago

3.1.39

2 years ago

3.1.41

2 years ago

3.1.40

2 years ago

3.1.34

2 years ago

3.1.33

2 years ago

3.1.36

2 years ago

3.1.35

2 years ago

3.1.30

2 years ago

3.1.32

2 years 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

3 years ago

3.1.15

3 years ago

3.1.18

3 years ago

3.1.17

3 years ago

3.1.22

2 years ago

3.1.21

3 years ago

3.1.20

3 years ago

3.1.19

3 years ago

3.1.12

3 years ago

3.1.11

3 years ago

3.1.14

3 years ago

3.1.13

3 years ago

3.1.10

3 years ago

3.1.9

3 years ago

3.1.8

3 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.1.7

3 years ago

3.1.6

3 years ago

3.1.5

3 years ago

3.1.4

3 years ago

3.0.9

3 years ago

3.0.8

3 years ago

3.0.7

3 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

4 years ago

2.0.18

4 years ago

2.0.17

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.51

4 years ago

1.0.50

4 years ago

1.0.49

4 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

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 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

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago