# 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)

*** ** * ** ***


```
public final class GPUBuffer implements AutoCloseable
```

<br />

*** ** * ** ***

A block of memory accessible by the GPU.

## Summary

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

## Public methods

### close

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

```
public external void close()
```

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
public final external void destroy()
```

Destroys the buffer and releases its resources.

### equals

```
public boolean equals(Object other)
```

### getConstMappedRange

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

```
@FastNative
public final external @NonNull ByteBuffer getConstMappedRange(long offset, long size)
```

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

| Parameters |
|---|---|
| `long offset` | The offset in bytes from the beginning of the buffer. |
| `long size` | The size in bytes of the range to get. |

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

### getHandle

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

```
public final long getHandle()
```

### getMapState

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

```
@FastNative
public final external int getMapState()
```

Gets the current mapping state of the buffer.

Value is one of the following:

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

<br />

| Returns |
|---|---|
| `int` | The buffer map state. |

### getMappedRange

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

```
@FastNative
public final external @NonNull ByteBuffer getMappedRange(long offset, long size)
```

Gets a pointer to the mapped range of the buffer.

| Parameters |
|---|---|
| `long offset` | The offset in bytes from the beginning of the buffer. |
| `long size` | The size in bytes of the range to get. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull 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
public final external long getSize()
```

Gets the size of the buffer in bytes.

| Returns |
|---|---|
| `long` | The buffer size. |

### getUsage

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

```
@FastNative
public final external int getUsage()
```

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 |
|---|---|
| `int` | The buffer usage flags. |

### hashCode

```
public int hashCode()
```

### mapAndAwait

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

```
public final void mapAndAwait(int mode, long offset, long size)
```

Asynchronously maps a range of the buffer for CPU access.

| Parameters |
|---|---|
| `int mode` | The mapping mode (read or write). Value is either `0` or a combination of the following: - `0` - `1` - `2` <br /> |
| `long offset` | The offset in bytes from the beginning of the buffer. |
| `long size` | 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
public final external void mapAsync(
    int mode,
    long offset,
    long size,
    @NonNull Executor callbackExecutor,
    @NonNull GPURequestCallback<Unit> callback
)
```

Asynchronously maps a range of the buffer for CPU access.

### mapState

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

```
public final int mapState()
```

### readMappedRange

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

```
@FastNative
public final external void readMappedRange(long offset, @NonNull ByteBuffer data)
```

Copies data from a mapped range into a destination pointer.

| Parameters |
|---|---|
| `long offset` | The offset in bytes from the beginning of the buffer. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/nio/ByteBuffer.html data` | The destination pointer. |

| Returns |
|---|---|
| `void` | 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
public final external void setLabel(@NonNull String label)
```

Sets a human-readable label for debugging.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html label` | The label string. |

### size

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

```
public final long size()
```

### unmap

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

```
@FastNative
public final external void unmap()
```

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

### usage

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

```
public final int usage()
```

### writeMappedRange

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

```
@FastNative
public final external void writeMappedRange(long offset, @NonNull ByteBuffer data)
```

Copies data from a source pointer into a mapped range.

| Parameters |
|---|---|
| `long offset` | The offset in bytes from the beginning of the buffer. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/nio/ByteBuffer.html data` | The source pointer. |

| Returns |
|---|---|
| `void` | The status of the operation. |