# core/gpumemorytracker (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/gpumemorytracker/

GPU memory tracking. (counterpart of the original GpuMemoryTracker)
- The GlContext functions that create resources and set their sizes (tex_image_2d, tex_storage_2d, renderbuffer_storage_multisample, buffer_data_size and the like) call into this file and record bytes per object. Deleting removes the entry, and losing the context clears everything.
- Bytes are nominal values computed from format and size. (row alignment, tile memory and multisample compression chosen by the driver are not known; multisample renderbuffers multiply by the sample count)
- The owner is the top three frames of the creating call stack. (desktop captures the frames at creation and resolves the names when read; the web (wasm) has no stack and uses the creating source location)
- It is off by default. While off, the creation and sizing functions of GlContext only look at one flag and nothing runs per frame. It is turned on by EngineConfiguration::track_gpu_memory, Graphic::enable_gpu_memory_tracking or Engine::enable_gpu_memory_tracking, and counts what is created after that.

Example: Reading the GPU memory total with tracking turned on

```rust
use vanilla::{Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
engine.enable_gpu_memory_tracking();
if let Some(usage) = engine.read_gpu_memory_usage() {
	assert!(usage.total_bytes >= usage.canvas_bytes);
}
```

## GpuMemoryKindUsage (struct) (Clone, Copy, PartialEq, Eq, Default, Debug)

GPU memory of one kind. (the { count, bytes } of the original kindTable)

Example: The default of the kind slot is 0 items and 0 bytes

```rust
use vanilla::GpuMemoryKindUsage;

let usage = GpuMemoryKindUsage::default();
assert_eq!(usage.count, 0);
assert_eq!(usage.bytes, 0);
```

- `pub count: u64` (field, since 0.4.0): Number of live objects.
  - returns u64
- `pub bytes: u64` (field, since 0.4.0): Bytes.
  - returns u64

## GpuMemoryOwnerUsage (struct) (Clone, PartialEq, Eq, Debug)

GPU memory of one owner. (an entry of the original ownerList)

Example: Creating one owner slot

```rust
use vanilla::{GpuMemoryKind, GpuMemoryOwnerUsage};

let usage = GpuMemoryOwnerUsage {
	owner: String::from("Graphic::new"),
	kind: GpuMemoryKind::Texture,
	count: 2,
	bytes: 4096,
};
assert_eq!(usage.kind, GpuMemoryKind::Texture);
assert_eq!(usage.bytes, 4096);
```

- `pub owner: String` (field, since 0.4.0): Owner. (the top three frames of the creating call stack joined with " < ")
  - returns String
- `pub kind: GpuMemoryKind` (field, since 0.4.0): Kind.
  - returns GpuMemoryKind
- `pub count: u64` (field, since 0.4.0): Object count.
  - returns u64
- `pub bytes: u64` (field, since 0.4.0): Bytes.
  - returns u64

## GpuMemoryUsage (struct) (Clone, PartialEq, Eq, Debug)

The GPU memory in use now. (what the original readGpuMemoryUsage gives: { totalBytes, canvasBytes, kindTable, ownerList })

Example: Read the total if the engine has graphics

```rust
use vanilla::{Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
if let Some(usage) = engine.read_gpu_memory_usage() {
	assert!(usage.total_bytes >= usage.canvas_bytes);
}
```

- `pub total_bytes: u64` (field, since 0.4.0): Total. (including the canvas default framebuffer share)
  - returns u64
- `pub canvas_bytes: u64` (field, since 0.4.0): Share of the canvas default framebuffer. (4 bytes color + 4 bytes depth and stencil; when multisampled, multiplied by the sample count 4 plus one resolved color image)
  - returns u64
- `pub kind_table: BTreeMap<GpuMemoryKind, GpuMemoryKindUsage>` (field, since 0.4.0): Share by kind. (all three kinds have an entry)
  - returns BTreeMap<GpuMemoryKind, GpuMemoryKindUsage>
- `pub owner_list: Vec<GpuMemoryOwnerUsage>` (field, since 0.4.0): Share by owner. (largest first)
  - returns Vec<GpuMemoryOwnerUsage>

## GpuMemoryKind (enum) (Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)

GPU memory kind. (original GpuMemoryKind)

- `Texture`: Texture.
- `Renderbuffer`: Renderbuffer. (color and depth of a multisample render target)
- `Buffer`: Buffer. (vertex, index, instance, uniform)

- `pub fn get_name(self) -> &'static str` (method, since 0.4.0): Kind name. (the value of the original GpuMemoryKind: "texture", "renderbuffer", "buffer")
  - returns &'static str: The name text.

  Example: Read the kind name

  ```rust
  use vanilla::GpuMemoryKind;
  
  assert_eq!(GpuMemoryKind::Texture.get_name(), "texture");
  ```

