|
| 1 | +# WebGL-Memory |
| 2 | + |
| 3 | +This a WebGL-Memory tracker. You add the script to your page |
| 4 | +before you initialize WebGL and then for a given context |
| 5 | +you can ask how much WebGL memory you're using. |
| 6 | + |
| 7 | +Note: This is only a guess as various GPUs have different |
| 8 | +internal requirements. For example a GPU might require that |
| 9 | +RGB be expanded internally to RGBA. Similarly a GPU might |
| 10 | +have alignment requirements. Still, this is likely to give |
| 11 | +a reasonable approximation. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +```html |
| 16 | +<script src="https://greggman.github.io/webgl-memory/webgl-memory.js" crossorigin></script> |
| 17 | +``` |
| 18 | + |
| 19 | +or |
| 20 | + |
| 21 | +```js |
| 22 | +import 'https://greggman.github.io/webgl-memory/webgl-memory.js'; |
| 23 | +``` |
| 24 | + |
| 25 | +Then in your code |
| 26 | + |
| 27 | +```js |
| 28 | +const ext = gl.getExtension('GMAN_webgl_memory'); |
| 29 | +... |
| 30 | +if (ext) { |
| 31 | + const info = ext.getMemoryInfo(); |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The info returned is |
| 36 | + |
| 37 | +```js |
| 38 | +{ |
| 39 | + memory: { |
| 40 | + buffer: <bytes used by buffers> |
| 41 | + texture: <bytes used by textures> |
| 42 | + renderbuffer: <bytes used by renderbuffers> |
| 43 | + drawingbuffer: <bytes used by the canvas> |
| 44 | + total: <bytes used in total> |
| 45 | + }, |
| 46 | + resource: { |
| 47 | + buffer: <count of buffers>, |
| 48 | + renderbuffer: <count of renderbuffers> |
| 49 | + program: <count of programs> |
| 50 | + query: <count of query objects, WebGL2 only> |
| 51 | + sampler: <count of samplers, WebGL2 only> |
| 52 | + shader: <count of shaders> |
| 53 | + sync: <count of sync objects, WebGL2 only> |
| 54 | + texture: <count of textures> |
| 55 | + transformFeedback: <count of transformfeedbacks, WebGL2 only> |
| 56 | + vertexArrays: <count of vertexArrays, only if used or WebGL2> |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Caveats |
| 62 | + |
| 63 | +1. You must have WebGL error free code. |
| 64 | + |
| 65 | + If your code is generating WebGL errors you must fix those first |
| 66 | + before using this library. Consider using [webgl-lint](https://greggman.github.io/webgl-lint) to help find your errors. |
| 67 | + |
| 68 | +2. Resource reference counting is not supported. |
| 69 | + |
| 70 | + In WebGL if you delete a WebGLObject (a buffer, a texture, etc..), |
| 71 | + then, if that object is still attached to something else (a buffer |
| 72 | + attached to a vertex array, a texture attached to a framebuffer), |
| 73 | + a shader attached to a program, the object is not actually deleted |
| 74 | + until it's detached or the thing it's attached to is itself deleted. |
| 75 | + |
| 76 | + Tracking all of that in JavaScript is more work than I was willing |
| 77 | + to put in ATM. My belief is that the stuff that is still attached |
| 78 | + is usually not a problem because either (a) you'll delete the objects |
| 79 | + that are holding the attachments (b) you'll detach the attachments |
| 80 | + by binding new ones (c) you have a leak where you're creating more and |
| 81 | + more of these objects the hold attachments in which case you can find |
| 82 | + the issue by watching your resources counts climb. |
| 83 | + |
| 84 | + Given that it seemed okay to skip this for now. |
| 85 | + |
| 86 | +## Example: |
| 87 | + |
| 88 | +[Click for Example]() |
| 89 | + |
| 90 | +## Opinion |
| 91 | + |
| 92 | +I'm not convinced this is the right way to do this. If I was making a |
| 93 | +webgl app and I wanted to know this stuff I'd track it myself by wrapping |
| 94 | +my own creation functions. |
| 95 | + |
| 96 | +In other words, lets say I wanted to know how many times I call |
| 97 | +`fetch`. |
| 98 | + |
| 99 | +```js |
| 100 | +const req = await fetch(url); |
| 101 | +const text = await req.text(); |
| 102 | +``` |
| 103 | + |
| 104 | +I'd just refactor that |
| 105 | + |
| 106 | +```js |
| 107 | +let fetchCount = 0; |
| 108 | +function doFetch(url) { |
| 109 | + fetchCount++; |
| 110 | + return fetch(url); |
| 111 | +} |
| 112 | + |
| 113 | +... |
| 114 | +const req = await doFetch(url); |
| 115 | +const text = await req.text(); |
| 116 | +``` |
| 117 | + |
| 118 | +No need for some fancy library. Simple. |
| 119 | + |
| 120 | +I could do similar things for WebGL functions. |
| 121 | + |
| 122 | +```js |
| 123 | +let textureCount = 0; |
| 124 | +function makeTexture(gl) { |
| 125 | + textureCount++; |
| 126 | + return gl.createTexture(gl); |
| 127 | +} |
| 128 | +function freeTexture(gl, tex) { |
| 129 | + --textureCount; |
| 130 | + gl.deleteTexture(tex); |
| 131 | +} |
| 132 | + |
| 133 | +const tex = makeTexture(gl); |
| 134 | +... |
| 135 | +freeTexture(gl, tex); |
| 136 | +``` |
| 137 | + |
| 138 | +Wrapping things yourself can have all kinds of benefits. |
0 commit comments