# core/gpumemorytracker (vanilla-engine 0.4.0)

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

GPU 메모리 셈. (원본 GpuMemoryTracker 의 짝)
- 텍스처, 렌더버퍼, 버퍼를 짓고 크기를 정하는 GlContext 의 함수(tex_image_2d, tex_storage_2d, renderbuffer_storage_multisample, buffer_data_size 같은 것)가
  이 파일의 길을 불러 객체마다 바이트를 적는다. 지우면 빼고, 컨텍스트를 잃으면 모두 비운다.
- 바이트는 형식과 크기로 셈한 이름값이다. (드라이버가 맞추는 줄 정렬, 타일 메모리, 멀티샘플 압축은 모른다. 멀티샘플 렌더버퍼는 샘플 수를 곱한다)
- 주인은 객체를 지은 호출 스택의 위쪽 세 칸이다. (데스크탑은 지을 때 스택 칸만 잡고 이름은 읽을 때 한 번 푼다. 웹(wasm)은 스택이 없어 지은 소스 자리로 대신한다)
- 기본은 꺼 둔다. 꺼져 있으면 GlContext 의 짓기, 크기 정하기 함수는 깃발 하나만 보고 지나가고 프레임마다 드는 일은 0 이다.
  켜는 길은 EngineConfiguration::track_gpu_memory, Graphic::enable_gpu_memory_tracking, Engine::enable_gpu_memory_tracking 이고 켠 뒤에 지은 것부터 센다.

예제: 추적을 켜고 쓰는 GPU 메모리 합 읽기

```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 메모리. (원본 kindTable 의 { count, bytes })

예제: 종류 칸의 기본값은 0 개 0 바이트

```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): 살아 있는 객체 수.
  - returns u64
- `pub bytes: u64` (field, since 0.4.0): 바이트.
  - returns u64

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

주인 하나의 GPU 메모리. (원본 ownerList 의 칸)

예제: 주인 칸 하나 만들기

```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): 주인. (지은 호출 스택의 위쪽 세 칸을 " < " 로 이은 이름)
  - returns String
- `pub kind: GpuMemoryKind` (field, since 0.4.0): 종류.
  - returns GpuMemoryKind
- `pub count: u64` (field, since 0.4.0): 객체 수.
  - returns u64
- `pub bytes: u64` (field, since 0.4.0): 바이트.
  - returns u64

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

지금 쓰는 GPU 메모리. (원본 readGpuMemoryUsage 가 주는 { totalBytes, canvasBytes, kindTable, ownerList })

예제: 엔진이 그래픽을 가졌으면 합을 읽는다

```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): 합. (캔버스 기본 프레임 버퍼 몫 포함)
  - returns u64
- `pub canvas_bytes: u64` (field, since 0.4.0): 캔버스 기본 프레임 버퍼 몫. (색 4 바이트 + 깊이와 스텐실 4 바이트, 멀티샘플이면 샘플 수 4 를 곱하고 풀어 낸 색 한 장을 더한다)
  - returns u64
- `pub kind_table: BTreeMap<GpuMemoryKind, GpuMemoryKindUsage>` (field, since 0.4.0): 종류별 몫. (세 종류 모두 칸이 있다)
  - returns BTreeMap<GpuMemoryKind, GpuMemoryKindUsage>
- `pub owner_list: Vec<GpuMemoryOwnerUsage>` (field, since 0.4.0): 주인별 몫. (큰 것부터)
  - returns Vec<GpuMemoryOwnerUsage>

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

GPU 메모리 종류. (원본 GpuMemoryKind)

- `Texture`: 텍스처.
- `Renderbuffer`: 렌더버퍼. (멀티샘플 렌더 타겟의 색, 깊이)
- `Buffer`: 버퍼. (정점, 인덱스, 인스턴스, 유니폼)

- `pub fn get_name(self) -> &'static str` (method, since 0.4.0): 종류 이름. (원본 GpuMemoryKind 의 값 "texture", "renderbuffer", "buffer")
  - returns &'static str: 이름 글.

  예제: 종류 이름 읽기

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

