• Stars
    star
    200
  • Rank 195,325 (Top 4 %)
  • Language
    C
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WebAssembly based Matrix and Vector library

GLMW - WebAssembly powered Matrix and Vector library


Description

This is an experimental near 1:1 port of gl-matrix v2.4.0 to WebAssembly.

Performance

In many cases glmw runs more than twice as fast as gl-matrix.

Some methods like *.str and *.equals are bridged and bring in some extra overhead.

Creating views with *.view is cheap, because they return a typed subarray of the WebAssembly module's memory buffer.

Benchmarks

Limitations

  • This library requires async instantiation, since WebAssembly has a synchronous buffer instantiation size limitation.
  • You need to manually free data, since there is no garbage collection yet (be careful! :p).
  • Methods like mat4.create and mat4.multiply return a numeric address. To get an view on your data you need to use e.g. mat4.view(address). This returns a Float32Array which is a direct view onto the allocated data in WebAssembly's memory. You can manually read/write from this view.
  • WebAssembly's memory cannot be directly shared with JavaScript's memory. This means that you cannot pass an JavaScript array into methods like vec3.sqrLength. You first have to convert it into the given module type (e.g. vec3.fromValues) which then gives you the memory address of the allocated data.
  • There is some overhead when calling from JavaScript->WebAssembly, but it seems acceptable. Slight performance drops are noticeable when calling a function more than ~15.000 times.

Bridged methods

  • *.str so a JavaScript String is returned.
  • *.equals so a JavaScript Boolean is returned.
  • *.exactEquals (see *.equals).

New methods

  • *.view lets you create views onto your data in WebAssembly's memory.
  • *.free to free data from WebAssembly's memory.

What is left

API modules

  • mat2
  • mat2d
  • mat3
  • vec2
  • quat

Installation

npm install glmw

or the browser distribution from here.

Instantiation

Before being able to use the library, you first have to call it's init method which then asynchronously compiles the WebAssembly module.

If you call a glmw function before it got instantiated somewhere, then a TypeError is thrown, because the function is simply not compiled yet.

Browser

This builds and compiles the WebAssembly module.

<script src="//rawgit.com/maierfelix/glmw/master/dist/glmw-browser.js"></script>
glmw.init().then(ready => {
  // glmw is now ready and can be used anywhere
});

ES

Import and call the init method in your main file. Afterwards you can use glmw anywhere.

index.js

import { init, vec3 } from "glmw";
import calc from "./calc";

init().then(ready => {
  // glmw is now ready and can be used anywhere
  calc();
});

calc.js

import { mat4 } from "glmw";
export default function() {
  return mat4.create();
};

Node

Require and call the init method in your main file. Afterwards you can use glmw anywhere.

index.js

const { init, vec3 } = require("glmw");
const calc = require("./calc");

init().then(ready => {
  // glmw is now ready and can be used anywhere
  calc();
});

calc.js

const { mat4 } = require("glmw");
module.exports = function() {
  return mat4.create();
};

Usage

Simple example

As you can see here, the API didn't really change.

let a = vec3.create();
let b = vec3.fromValues(1.0, 2.0, 3.0);
vec3.add(a, a, b);
console.log( vec3.view(a) ); // Float32Array(3) [1, 2, 3]

Creating views

First, this is what glmw returns to you. Instead of references, only the numeric addresses are returned:

a = mat4.create();          // 65688
b = mat4.create();          // 65760
c = mat4.multiply(a, a, b); // 65688

You can change data by hand this way:

vA = mat4.view(a); // Float32Array([1, 0, 0...])
vA[0] = 2;         // you can now read/write
vA;                // Float32Array([2, 0, 0...])

Freeing data

Since WebAssembly doesn't have garbage collection yet, you have to be careful when and where you allocate data. You can free data by calling *.free:

a = mat4.create(); // allocate data for a
mat4.free(a);      // a's data is now freed

More Repositories

1

poxi

A flat pixel art editor
JavaScript
2,479
star
2

Iroh

Dynamic code analysis tool - Exploit, record and analyze running JavaScript
JavaScript
921
star
3

nvk

Vulkan API for JavaScript/TypeScript
C++
915
star
4

PokeMMO

🎮 Pokemon MMO engine with realtime editor
JavaScript
728
star
5

POGOserver

Pokemon GO server emulator
JavaScript
460
star
6

azula

Lightweight GPU accelerated HTML GUI for native JavaScript
C
320
star
7

dawn-ray-tracing

Hardware Ray tracing extension for Chromium WebGPU
C++
263
star
8

wasm-particles

WASM accelerated particles
JavaScript
253
star
9

mini-c

C to WebAssembly compiler
JavaScript
250
star
10

webgpu

WebGPU for Node [Deprecated, Unmaintained]
C
243
star
11

rokon

3D engine using WebGL2, WebAssembly
JavaScript
233
star
12

mini-js

Minimal self-hosted JavaScript compiler in 1k lines of code
JavaScript
158
star
13

WebGPU-Path-Tracer

Toy path tracer using WebGPU RT
JavaScript
135
star
14

tiny-rtx

VK_NV_ray_tracing based Ray tracer
JavaScript
100
star
15

TXT2PNG

🗻 Store ASCII inside PNGs
JavaScript
73
star
16

momo

A Vulkan RTX Path Tracer
JavaScript
69
star
17

hevia-parser

A recursive descent Swift parser written in ES6
JavaScript
63
star
18

nvk-examples

Examples and demos for nvk
JavaScript
58
star
19

chromium-ray-tracing

Chromium build with hardware accelerated ray tracing
57
star
20

pokemon-emerald-rom-hacking

ROM hacking kit for Pokemon Emerald
JavaScript
50
star
21

VK_KHR_ray_tracing

VK_KHR_ray_tracing example
C++
42
star
22

YUE

My personal WebGPU based 3D renderer
TypeScript
38
star
23

html-canvas

Rendering HTML into canvas
JavaScript
26
star
24

canvas-live-stream

Live streaming canvas over websockets
JavaScript
26
star
25

webgpu-examples

Examples for node-webgpu
JavaScript
24
star
26

hevia-compiler

Strongly typed language that compiles to JavaScript
JavaScript
17
star
27

toy-compiler

A tiny self-hosted compiler
JavaScript
16
star
28

emerald-engine

JavaScript
16
star
29

POGO-asset-downloader

🚀 Download Pokemon GO 3D models
JavaScript
16
star
30

Lamella

⚪ Flow based Visual Programming
JavaScript
12
star
31

tolw

WebGL .obj loader - WebAssembly port of tinyobjloader
JavaScript
11
star
32

src2game

Turn Javascript source code into a Game
JavaScript
10
star
33

nvk-optix-denoiser

Real-time Denoiser for node-vulkan
JavaScript
10
star
34

3d-code-vizard

3D runtime code visualization
JavaScript
9
star
35

object-reflector

Simple & powerful Object reflections
JavaScript
8
star
36

htmlparse

Minimal blazing fast HTML parser
JavaScript
7
star
37

space-shooter

A simple space shooter
JavaScript
7
star
38

banotils

Utilities for the banano cryptocurrency
TypeScript
6
star
39

bitpackr

An efficient low-level packet serializer, down to bit-level
TypeScript
6
star
40

bnsh-decoder

A decoder for Nintendo Switch BNSH shaders
C++
5
star
41

Watcher

Track all changes inside your DOM
JavaScript
5
star
42

webgpu-compute-rasterizer

A simple compute based rasterizer
JavaScript
5
star
43

PokeName

Fiddle with Pokémon names in different languages
JavaScript
4
star
44

Calc

Browserbased Excel
JavaScript
4
star
45

xr-engine

WebXR engine
JavaScript
3
star
46

POGOdecode

Pokemon GO Request<->Response decoder
JavaScript
3
star
47

nvk-essentials

Essential development tools for nvk
GLSL
3
star
48

Interpreter

A small interpreter with asynchronous step based execution support
JavaScript
2
star
49

wast-parser

WebAssembly WAST parser
JavaScript
2
star
50

neural-ocr

Neural network OCR
JavaScript
2
star
51

mc-spawner-upgrade

Lets you upgrade your mob spawners in minecraft
Java
2
star
52

pawtils

Utilities for the paw cryptocurrency
TypeScript
2
star
53

nanpow

TypeScript
1
star
54

Music-Box

Programmable wave music box
JavaScript
1
star
55

Expanding-spiral-animation

Canvas based expanding spiral experiment written in ES6
JavaScript
1
star