# GPUBuffer

Artifact: [androidx.webgpu:webgpu](https://developer.android.com/jetpack/androidx/releases/webgpu) [View Source](https://cs.android.com/search?q=file:androidx/webgpu/GPUBuffer.kt+class:androidx.webgpu.GPUBuffer) Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

*** ** * ** ***


```
class GPUBuffer : AutoCloseable
```

<br />

*** ** * ** ***

A block of memory accessible by the GPU.

## Summary

| ### Public functions |
|---|---|
| `open external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#close()()` Decrements the reference count of the object and frees resources when the count reaches zero. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#destroy()()` Destroys the buffer and releases its resources. |
| `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#equals(kotlin.Any)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` |
| `external https://developer.android.com/reference/java/nio/ByteBuffer.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#getConstMappedRange(kotlin.Long,kotlin.Long)(offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Gets a read-only pointer to the mapped range of the buffer. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#getMapState()()` Gets the current mapping state of the buffer. |
| `external https://developer.android.com/reference/java/nio/ByteBuffer.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#getMappedRange(kotlin.Long,kotlin.Long)(offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Gets a pointer to the mapped range of the buffer. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#getSize()()` Gets the size of the buffer in bytes. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#getUsage()()` Gets the allowed usages of the buffer. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#hashCode()()` |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#mapAndAwait(kotlin.Int,kotlin.Long,kotlin.Long)(mode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Asynchronously maps a range of the buffer for CPU access. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#mapAsync(kotlin.Int,kotlin.Long,kotlin.Long,java.util.concurrent.Executor,androidx.webgpu.GPURequestCallback)( mode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, callbackExecutor: https://developer.android.com/reference/java/util/concurrent/Executor.html, callback: https://developer.android.com/reference/kotlin/androidx/webgpu/GPURequestCallback<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> )` Asynchronously maps a range of the buffer for CPU access. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#readMappedRange(kotlin.Long,java.nio.ByteBuffer)(offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, data: https://developer.android.com/reference/java/nio/ByteBuffer.html)` Copies data from a mapped range into a destination pointer. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#setLabel(kotlin.String)(label: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Sets a human-readable label for debugging. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#unmap()()` Unmaps the buffer, making it accessible to the GPU again. |
| `external https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/dalvik/annotation/optimization/FastNative.html https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#writeMappedRange(kotlin.Long,java.nio.ByteBuffer)(offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, data: https://developer.android.com/reference/java/nio/ByteBuffer.html)` Copies data from a source pointer into a mapped range. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#handle()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#mapState()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#size()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/webgpu/GPUBuffer#usage()` |

## Public functions

### close

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
open external fun close(): Unit
```

Decrements the reference count of the object and frees resources when the count reaches zero.

This is the standard way to manage object lifetimes and should be used in `use` blocks. After calling this, the object is no longer usable.

### destroy

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun destroy(): Unit
```

Destroys the buffer and releases its resources.

### equals

```
open operator fun equals(other: Any?): Boolean
```

### getConstMappedRange

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun getConstMappedRange(offset: Long = 0, size: Long = Constants.WHOLE_MAP_SIZE): ByteBuffer
```

Gets a read-only pointer to the mapped range of the buffer.

| Parameters |
|---|---|
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = 0` | The offset in bytes from the beginning of the buffer. |
| `size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = Constants.WHOLE_MAP_SIZE` | The size in bytes of the range to get. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer.html` | A read-only pointer to the mapped memory. |

### getMapState

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun getMapState(): Int
```

Gets the current mapping state of the buffer.

Value is one of the following:

- `1`
- `2`
- `3`

<br />

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The buffer map state. |

### getMappedRange

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun getMappedRange(offset: Long = 0, size: Long = Constants.WHOLE_MAP_SIZE): ByteBuffer
```

Gets a pointer to the mapped range of the buffer.

| Parameters |
|---|---|
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = 0` | The offset in bytes from the beginning of the buffer. |
| `size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = Constants.WHOLE_MAP_SIZE` | The size in bytes of the range to get. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer.html` | A pointer to the mapped memory. |

### getSize

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun getSize(): Long
```

Gets the size of the buffer in bytes.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The buffer size. |

### getUsage

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun getUsage(): Int
```

Gets the allowed usages of the buffer.

Value is either `0` or a combination of the following:

- `0`
- `1`
- `2`
- `4`
- `8`
- `16`
- `32`
- `64`
- `128`
- `256`
- `512`

<br />

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The buffer usage flags. |

### hashCode

```
open fun hashCode(): Int
```

### mapAndAwait

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
suspend fun mapAndAwait(mode: Int, offset: Long, size: Long): Unit
```

Asynchronously maps a range of the buffer for CPU access.

| Parameters |
|---|---|
| `mode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The mapping mode (read or write). Value is either `0` or a combination of the following: - `0` - `1` - `2` <br /> |
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The offset in bytes from the beginning of the buffer. |
| `size: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The size in bytes of the range to map. |

### mapAsync

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun mapAsync(
    mode: Int,
    offset: Long,
    size: Long,
    callbackExecutor: Executor,
    callback: GPURequestCallback<Unit>
): Unit
```

Asynchronously maps a range of the buffer for CPU access.

### readMappedRange

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun readMappedRange(offset: Long, data: ByteBuffer): Unit
```

Copies data from a mapped range into a destination pointer.

| Parameters |
|---|---|
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The offset in bytes from the beginning of the buffer. |
| `data: https://developer.android.com/reference/java/nio/ByteBuffer.html` | The destination pointer. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The status of the operation. |

### setLabel

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun setLabel(label: String): Unit
```

Sets a human-readable label for debugging.

| Parameters |
|---|---|
| `label: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The label string. |

### unmap

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun unmap(): Unit
```

Unmaps the buffer, making it accessible to the GPU again.

### writeMappedRange

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
@FastNative
external fun writeMappedRange(offset: Long, data: ByteBuffer): Unit
```

Copies data from a source pointer into a mapped range.

| Parameters |
|---|---|
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The offset in bytes from the beginning of the buffer. |
| `data: https://developer.android.com/reference/java/nio/ByteBuffer.html` | The source pointer. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The status of the operation. |

## Public properties

### handle

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
val handle: Long
```

### mapState

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
val mapState: Int
```

### size

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
val size: Long
```

### usage

Added in [1.0.0-alpha06](https://developer.android.com/jetpack/androidx/releases/webgpu#1.0.0-alpha06)

```
val usage: Int
```