# core/graphic/gpumemorytracker (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/graphic/gpumemorytracker/

GPU memory tracking.
- Intercepts the functions that create textures, renderbuffers and buffers and set their sizes (texImage2D, texStorage2D, renderbufferStorageMultisample, bufferData and so on) on the context, and records bytes per object. Deleting subtracts, and losing the context clears everything.
- Bytes are nominal values computed from format and size. (Driver row alignment, tile memory and multisample compression are unknown. Multisample renderbuffers are multiplied by the sample count)
- The owner is the top three frames of the call stack that created the object. (A development name that tells which engine part created it, the stack is read once at creation)
- Objects dropped without deleting are removed by FinalizationRegistry when garbage collection comes. (The browser releases the GL object then)
- Only one is attached per context. (Off by default. Turn it on with EngineConfiguration.trackGpuMemory, Graphic.enableGpuMemoryTracking, or GpuMemoryTracker.attach for a context made outside the engine. Before it is attached, GL create calls are not intercepted)

Example: Reading the engine GPU memory by kind

```js
import { Engine, EngineConfiguration, Scene } from "./vanilla.js";
const engine = new Engine(new EngineConfiguration());
engine.run(new Scene());
const usage = engine.readGpuMemoryUsage();
console.log(usage.kindTable.texture.count, usage.totalBytes);
```

## GpuMemoryTracker extends Object

GPU memory tracker class.

Example: Reading the bytes of a created texture

```js
import { GpuMemoryTracker } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const gpuMemoryTracker = GpuMemoryTracker.attach(webGL2RenderingContext);
const texture = webGL2RenderingContext.createTexture();
webGL2RenderingContext.bindTexture(webGL2RenderingContext.TEXTURE_2D, texture);
webGL2RenderingContext.texStorage2D(webGL2RenderingContext.TEXTURE_2D, 1, webGL2RenderingContext.RGBA8, 16, 16);
console.log(gpuMemoryTracker.readUsage().kindTable.texture.bytes);
```

- `constructor(webGL2RenderingContext)` (constructor, since 0.4.0): Constructor. (Attach with attach)
  - `webGL2RenderingContext` WebGL2RenderingContext: The context to track.
- `static attach(webGL2RenderingContext)` (static, since 0.4.0): Attaches the tracker to a context. (Creates and attaches it if missing, only one per context)
  - `webGL2RenderingContext` WebGL2RenderingContext: The context.
  - returns GpuMemoryTracker: That context's GPU memory tracker.

  Example: Reading the GPU memory of a context made outside the engine

  ```js
  import { GpuMemoryTracker } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const gpuMemoryTracker = GpuMemoryTracker.attach(webGL2RenderingContext);
  const texture = webGL2RenderingContext.createTexture();
  webGL2RenderingContext.bindTexture(webGL2RenderingContext.TEXTURE_2D, texture);
  webGL2RenderingContext.texStorage2D(webGL2RenderingContext.TEXTURE_2D, 1, webGL2RenderingContext.RGBA8, 64, 64);
  const usage = gpuMemoryTracker.readUsage();
  console.log(usage.totalBytes);
  ```

- `readUsage()` (method, since 0.4.0): Reads the GPU memory in use now.
  - returns { totalBytes: number, canvasBytes: number, kindTable: object, ownerList: Array<{ owner: string, kind: string, count: number, bytes: number }> }: Total (including the canvas share), default framebuffer share of the canvas, per-kind { count, bytes }, per-owner shares (largest first).

  Example: Reading GPU memory by kind and by owner

  ```js
  import { GpuMemoryTracker } from "./vanilla.js";
  const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
  const gpuMemoryTracker = GpuMemoryTracker.attach(webGL2RenderingContext);
  const usage = gpuMemoryTracker.readUsage();
  console.log(usage.kindTable.texture.bytes, usage.ownerList.length);
  ```

## GpuMemoryKind (enum)

Type name.

Example: Reading GPU memory by kind

```js
import { GpuMemoryTracker, GpuMemoryKind } from "./vanilla.js";
const webGL2RenderingContext = document.createElement("canvas").getContext("webgl2");
const gpuMemoryTracker = GpuMemoryTracker.attach(webGL2RenderingContext);
const usage = gpuMemoryTracker.readUsage();
console.log(usage.kindTable[GpuMemoryKind.renderbuffer].bytes);
```

- `texture` = `texture`: Texture.
- `renderbuffer` = `renderbuffer`: Renderbuffer. (Color and depth of a multisample render target)
- `buffer` = `buffer`: Buffer. (Vertex, index, instance, uniform)
