• Stars
    star
    306
  • Rank 136,456 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

A library to track webgl-memory

WebGL-Memory

This is a WebGL-Memory tracker. You add the script to your page before you initialize WebGL and then for a given context you can ask how much WebGL memory you're using.

Note: This is only a guess as various GPUs have different internal requirements. For example a GPU might require that RGB be expanded internally to RGBA. Similarly a GPU might have alignment requirements. Still, this is likely to give a reasonable approximation.

Usage

<script src="https://greggman.github.io/webgl-memory/webgl-memory.js" crossorigin></script>

or

import 'https://greggman.github.io/webgl-memory/webgl-memory.js';

Then in your code

const ext = gl.getExtension('GMAN_webgl_memory');
...
if (ext) {
  const info = ext.getMemoryInfo();
}

The info returned is

{
  memory: {
    buffer: <bytes used by buffers>
    texture: <bytes used by textures>
    renderbuffer: <bytes used by renderbuffers>
    drawingbuffer: <bytes used by the canvas>
    total: <bytes used in total>
  },
  resources: {
    buffer: <count of buffers>,
    renderbuffer: <count of renderbuffers>
    program: <count of programs>
    query: <count of query objects, WebGL2 only>
    sampler: <count of samplers, WebGL2 only>
    shader: <count of shaders>
    sync: <count of sync objects, WebGL2 only>
    texture: <count of textures>
    transformFeedback: <count of transformfeedbacks, WebGL2 only>
    vertexArray: <count of vertexArrays, only if used or WebGL2>
  }
}

Caveats

  1. You must have WebGL error free code.

    If your code is generating WebGL errors you must fix those first before using this library. Consider using webgl-lint to help find your errors.

  2. Resource reference counting is not supported.

    In WebGL if you delete a WebGLObject (a buffer, a texture, etc..), then, if that object is still attached to something else (a buffer attached to a vertex array, a texture attached to a framebuffer, a shader attached to a program), the object is not actually deleted until it's detached or the thing it's attached to is itself deleted
    unless the thing it's attached to is currently bound. It's complicated 😭

    Tracking all of that in JavaScript is more work than I was willing to put in ATM. My belief is that the stuff that is still attached is usually not a problem because either (a) you'll delete the objects that are holding the attachments (b) you'll detach the attachments by binding new ones (c) you have a leak where you're creating more and more of these objects that hold attachments in which case you can find the issue by watching your resources counts climb.

    Given that it seemed okay to skip this for now.

  3. texImage2D/3D vs texStorage2D/3D

    Be aware that texImage2D/3D may require double the memory of texStorage2D/3D.

    Based on the design of texImage2D/3D, every mip level can have a different size/format and so until it's time to draw, there is no way to know if those levels will be updated by the app to be matching. Further, in WebGL2, there's no way to know before draw time if the app will set TEXTURE_BASE_LEVEL and TEXTURE_MAX_LEVEL to be a texture complete subset of mip levels.

    WebGL-memory does not report this difference because it's up to the implementation what really happens behind the scenes. In general though, texStorage2D/3D has a much higher probability of using less memory overall.

    The tradeoff for using texStorage is that the texture's size is immutable. So, for example, if you wanted to wrap a user's image to a cube, and then change that image when the user selects a different sized image, with texImage you can just upload the new image to the existing texture. With texStorage you'd be required to create a new texture.

  4. ELEMENT_ARRAY_BUFFER

    Buffers used with ELEMENT_ARRAY_BUFFER may need a second copy in ram. This is because WebGL requires no out of bounds memory access (eg, you have a buffer with 10 vertices but you have an index greater >= 10). This can be handled in 2 ways (1) if the driver advertizes "robustness" then rely on the driver (2) keep a copy of that data in ram and check before draw time that no indices are out of range.

    WebGL-memory does not report this difference because it's up to the implementation. Further, unlike the texture issue above there is nothing an app can do. Fortunately such buffers are usually a small percent of the data used by most WebGL apps.

Example:

Click here for an Example
Unity example here

Development

git clone https://github.com/greggman/webgl-memory.git
cd webgl-memory
npm install

now serve the folder

npx servez

and go to http://localhost:8080/test?src=true

src=true tells the test harness to use the unrolled source from the src folder where as without it uses webgl-memory.js in the root folder which is built using npm run build.

grep=<some expression> will limit the tests as in ...?src=true&grep=renderbuffer only runs the tests with renderbuffer in their description.

Live Tests

built version
source version

Thoughts

I'm not totally convinced this is the right way to do this. If I was making a webgl app and I wanted to know this stuff I think I'd track it myself by wrapping my own creation functions.

In other words, lets say I wanted to know how many times I call fetch.

const req = await fetch(url);
const text = await req.text();

I'd just refactor that

let fetchCount = 0;
function doFetch(url) {
  fetchCount++;
  return fetch(url);
}

...
const req = await doFetch(url);
const text = await req.text();

No need for some fancy library. Simple.

I could do similar things for WebGL functions.

let textureCount = 0;
function makeTexture(gl) {
  textureCount++;
  return gl.createTexture(gl);
}
function freeTexture(gl, tex) {
  --textureCount;
  gl.deleteTexture(tex);
}

const tex = makeTexture(gl);
...
freeTexture(gl, tex);

Also, even if webgl-memory is an okay way to do it I'm not sure making it an extension was the best way vs just some library you call like webglMemoryTracker.init(someWebGLRenderingContext). I structured it this way just because I used webgl-lint as the basis to get this working.

Licence

MIT

More Repositories

1

twgl.js

A Tiny WebGL helper Library
JavaScript
2,451
star
2

better-unity-webgl-template

A better default template for Unity WebGL
HTML
630
star
3

HappyFunTimes

A System for creating 10-100+ player local games
JavaScript
371
star
4

html5bytebeat

Bytebeats in HTML5
JavaScript
369
star
5

tdl

A low-level WebGL library
JavaScript
280
star
6

ffmpegserver.js

Receives canvas frames from browser to generate video on the server. Compatible with CCapture.js
JavaScript
267
star
7

servez

A simple web server for local web development.
JavaScript
258
star
8

hsva-unity

A Hue Saturation Value adjustment shader for Unity. Useful for making lots of character colors.
GLSL
202
star
9

webgl-lint

Checks your WebGL usage for common issues
JavaScript
160
star
10

wgpu-matrix

Fast WebGPU 3d math library
JavaScript
129
star
11

virtual-webgl

Virtualize WebGL Contexts
JavaScript
105
star
12

unzipit

Random access unzip library for JavaScript
JavaScript
100
star
13

unity-webgl-copy-and-paste

Support Copy and Paste in Unity WebGL
C#
90
star
14

doodles

Random JavaScript doodles
JavaScript
58
star
15

getuserimage-unity-webgl

How to ask the user for a photo in Unity-WebGL
C#
55
star
16

webgl-helpers

some tiny webgl scripts that might come in handy
JavaScript
54
star
17

webgpu-memory

Track your WebGPU memory usage
JavaScript
43
star
18

servez-cli

The cli version of servez
JavaScript
38
star
19

ImHUI

Experimental UI
TypeScript
34
star
20

webgl-capture

code to help make a reduced test case for WebGL by capturing the commands and generating a stand alone program
JavaScript
34
star
21

oes-vertex-array-object-polyfill

WebGL OES_vertex_array_object polyfill for GPUs/Drivers/Browsers that don't have it
JavaScript
33
star
22

hft-unity3d

Unity3D Libraries for HappyFunTimes
C#
31
star
23

requestanimationframe-fix.js

Fix for requestAnimationFrame with lots of elements
JavaScript
31
star
24

webgpu-utils

Some helpers for webgpu
JavaScript
30
star
25

youtube_chromecast_speed_hack

A way to play a youtube video on Chromecast with settable speed
HTML
28
star
26

pico-8-post-processing

post process pico-8
JavaScript
28
star
27

hft-tonde-iko

A Multi-Machine Platformer
JavaScript
24
star
28

react-split-it

A React Based Splitter
JavaScript
24
star
29

pixel-perfect.js

Display Image Pixel Perfect
HTML
22
star
30

gradient-editor

A Jquery based gradient editor
JavaScript
20
star
31

jsgist

A code playground that stores data as github gists
JavaScript
16
star
32

interval-timer

A Simple Interval Timer
JavaScript
16
star
33

html5-gamepad-test

HTML
15
star
34

webgpu-avoid-redundant-state-setting

Check for and avoid redundant state setting
JavaScript
15
star
35

webgl-canvas-2d

A minimal implementation of the canvas 2D API through WebGL
JavaScript
14
star
36

rockfall

Rockfall. A game where rocks fall
JavaScript
14
star
37

jsbenchit

A JavaScript benchmark static page website that uses gists to store benchmarks
JavaScript
14
star
38

fanfictionreader

Reads your fanfiction to you.
JavaScript
13
star
39

happyfuntimes.net

The HappyFunTimes.net code
JavaScript
10
star
40

dekapng

Make giant PNG files in the browser
TypeScript
10
star
41

hft-gamepad-api

Emulates the HTML5 Gamepad API using smartphones and HappyFunTimes
JavaScript
10
star
42

hft-boomboom

A happyfuntimes game with splosions
JavaScript
9
star
43

DeJson.NET

A simple serialization library from JSON to C# classes based on MiniJSON good for Unity3D
C#
9
star
44

oculus-anti-spy

Try top stop Facebook from spying on all Oculus Activity
JavaScript
8
star
45

uzip-module

An ES6 module version of UZIP.js
JavaScript
8
star
46

imageutils

A few image utils for in browser JavaScript
JavaScript
7
star
47

hft-unity-gamepad

A Generic HappyFunTimes Gamepad for Unity
JavaScript
7
star
48

MoPho-V

A Community Supported Movie and Photo Viewer
JavaScript
6
star
49

sharks-with-frickin-lasers

JavaScript
6
star
50

hft-clean

The simplest happyfuntimes example, no other scripts
JavaScript
6
star
51

webgpu-helpers

Small scripts useful when debugging or developing webgpu
JavaScript
6
star
52

octopus

JavaScript
5
star
53

audiostreamsource.js

Provides a streamed audio source for WebAudio across browsers
JavaScript
5
star
54

dump-all-the-shaders

A script you can add to dump all your shaders to the console.
JavaScript
4
star
55

epub-viewer

A simple epub viewer. Client side only
HTML
4
star
56

image-grid

A simple image-grid for displaying images um, in a grid in the browser
JavaScript
4
star
57

simple-new-tab-page

A simple new tab page extension
JavaScript
4
star
58

other-window-ipc

IPC between windows in Electron
JavaScript
4
star
59

macos-opengl-experiments

Simple OpenGL stuff on MacOS
Objective-C++
3
star
60

rest-url

Makes REST urls
JavaScript
3
star
61

aws-oauth-helper

An AWS Lambda function to handle the oauth client secret part of oauth
JavaScript
3
star
62

unity-load-mp3-at-runtime

example of loading mp3 at runtime
C#
3
star
63

soundcloud-audio-reactive-example

Soundcloud audio reactive example using new API
JavaScript
3
star
64

hft-unityvideofromunity

An example of sending WebCam video FROM unity to the controller (phones)
C#
3
star
65

muigui

baking
JavaScript
3
star
66

hft-syncthreejs

Shows syncing a three.js example across multiple machines using HappyFunTimes
JavaScript
3
star
67

fixallthetags

SO script to fix tags
JavaScript
3
star
68

hft-local

Run a HappyFunTimes game without HappyFunTimes (no networking .. sometimes good for demos)
JavaScript
3
star
69

screenshot-ftw

screenshot a window across OSes
C++
3
star
70

webgl-benchmarks

WebGL Benchmarks (NOT GPU BENCHMARKS!!!)
JavaScript
3
star
71

opengl-fundamentals

JavaScript
3
star
72

stackoverflow-getallanswers

Get all answers for a particular user (and all the questions for those answers)
Python
3
star
73

hft-unitysimple

The simplest Unity example for HappyFunTimes using C#
C#
3
star
74

jsgistrunner

JavaScript
2
star
75

fisheye-skybox-unity

Make a fisheye skybox shader in unity
ShaderLab
2
star
76

cssparse.net

CSS string to Unity3D Color parser
C#
2
star
77

u2b-ux

better youtube ux
JavaScript
2
star
78

hft-unity-character-select

A HappyFunTimes Unity example showing spawning different prefabs based on player character selection
C#
2
star
79

native-msg-box

Allows you to display a native MessageBox / Dialog from node.js
JavaScript
2
star
80

hft-simple

A simple example for HappyFunTimes
JavaScript
2
star
81

hft-jumpjump

The HappyFunTimes JumpJump Example Platformer
JavaScript
2
star
82

servez-lib

The server part of servez
JavaScript
2
star
83

check-all-the-errors

load all your pages, check for javascript errors
JavaScript
2
star
84

hft-unity-2-button-gamejam

A Unity HappyFunTimes Template for the Pico Pico Cafe 2 Button Gamejam
C#
2
star
85

hft-simple-no-electron

an example of using happyfuntimes without electron
JavaScript
2
star
86

eslint-plugin-one-variable-per-var

Enforce one variable declaration per var statement
JavaScript
2
star
87

ldcp

low dependencies cp for node
JavaScript
2
star
88

LUT-to-PNG

Convert a LUT or CUBE file to a PNG (for Unreal / Unity)
JavaScript
2
star
89

hft-powpow

A simple space shooter game for HappyFunTimes
JavaScript
1
star
90

hft-utils

Various JavaScript files shared among HappyFunTimes example games
JavaScript
1
star
91

vertexshaderart.org

vertexshaderart.org
1
star
92

hft-sync2d

Example showing syncing canvas 2d across machines using HappyFunTimes
JavaScript
1
star
93

dns-server

Automatically exported from code.google.com/p/dns-server
C++
1
star
94

bloom

look at the source
JavaScript
1
star
95

hft-unity-cardboard

Example of using HappyFunTimes with Unity and Google Cardbard
C#
1
star
96

webgpu-dev-extension

Explorational WebGPU Dev Extension
JavaScript
1
star
97

font-utils

Some font utils I wrote to generate fonts for gamemaker
C
1
star
98

jsbenchit-comments

just a point to host jsbenchit comments on another domain for security
HTML
1
star
99

hft-c

HappyFunTimes support for C / C++ based games
C++
1
star
100

hft-exe

HappyFunTimes executable creator
C
1
star