# WriteBuffer

> Asynchronously copies data from host memory to a device buffer.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.LightTransport](/engine/6000.5/script-reference/unityengine/lighttransport.md)
* **Assembly:** UnityEngine.CoreModule

## WriteBuffer\<T>(BufferSlice\<T>, NativeArray\<T>)

Asynchronously copies data from host memory to a device buffer.

```csharp
void WriteBuffer<T>(BufferSlice<T> dst, NativeArray<T> src) where T : struct
```

### Parameters

**** (\[BufferSlice\<T>]\(/engine/6000.5/script-reference/unityengine/lighttransport/bufferslice1)): The [BufferSlice](/engine/6000.5/script-reference/unityengine/lighttransport/bufferslice1.md) specifying the destination on the device.**** (\[NativeArray\<T>]\(/engine/6000.5/script-reference/unity/collections/nativearray1)): The source array in CPU memory. Must remain valid until the operation completes.

### Remarks

Initiates an asynchronous transfer of data from host memory to device memory. The method returns immediately after enqueuing the operation. Use [IDeviceContext.Flush()](/engine/6000.5/script-reference/unityengine/lighttransport/idevicecontext/flush.md) to ensure the operation begins executing, then use [IDeviceContext.Wait](/engine/6000.5/script-reference/unityengine/lighttransport/idevicecontext/wait.md) or [IDeviceContext.IsCompleted](/engine/6000.5/script-reference/unityengine/lighttransport/idevicecontext/iscompleted.md) with the provided event to determine when the transfer is complete.

The source [NativeArray\<T>](/engine/6000.5/script-reference/unity/collections/nativearray1.md) must remain allocated and unchanged until the write operation completes, as indicated by the event.

**Warning:** [EventID](/engine/6000.5/script-reference/unityengine/lighttransport/eventid.md) instances are single-use. Do not reuse an event that has been passed to this method.

### Examples

```csharp
// Prepare data and write to buffer
using var inputData = new NativeArray<float>(512, Allocator.Persistent);
for (int i = 0; i < inputData.Length; i++)
    inputData[i] = i * 0.5f;

var writeEvent = context.CreateEvent();
context.WriteBuffer(bufferSlice, inputData, writeEvent);
context.Flush();

// Wait for write to complete before using the buffer
context.Wait(writeEvent);
context.DestroyEvent(writeEvent);
```

## WriteBuffer\<T>(BufferSlice\<T>, NativeArray\<T>, EventID)

Asynchronously copies data from host memory to a device buffer.

```csharp
void WriteBuffer<T>(BufferSlice<T> dst, NativeArray<T> src, EventID id) where T : struct
```

### Parameters

**** (\[BufferSlice\<T>]\(/engine/6000.5/script-reference/unityengine/lighttransport/bufferslice1)): The [BufferSlice](/engine/6000.5/script-reference/unityengine/lighttransport/bufferslice1.md) specifying the destination on the device.**** (\[NativeArray\<T>]\(/engine/6000.5/script-reference/unity/collections/nativearray1)): The source array in CPU memory. Must remain valid until the operation completes.**** (\[EventID]\(/engine/6000.5/script-reference/unityengine/lighttransport/eventid)): Optional event ID to track completion of the write operation.

### Remarks

Initiates an asynchronous transfer of data from host memory to device memory. The method returns immediately after enqueuing the operation. Use [IDeviceContext.Flush()](/engine/6000.5/script-reference/unityengine/lighttransport/idevicecontext/flush.md) to ensure the operation begins executing, then use [IDeviceContext.Wait](/engine/6000.5/script-reference/unityengine/lighttransport/idevicecontext/wait.md) or [IDeviceContext.IsCompleted](/engine/6000.5/script-reference/unityengine/lighttransport/idevicecontext/iscompleted.md) with the provided event to determine when the transfer is complete.

The source [NativeArray\<T>](/engine/6000.5/script-reference/unity/collections/nativearray1.md) must remain allocated and unchanged until the write operation completes, as indicated by the event.

**Warning:** [EventID](/engine/6000.5/script-reference/unityengine/lighttransport/eventid.md) instances are single-use. Do not reuse an event that has been passed to this method.

### Examples

```csharp
// Prepare data and write to buffer
using var inputData = new NativeArray<float>(512, Allocator.Persistent);
for (int i = 0; i < inputData.Length; i++)
    inputData[i] = i * 0.5f;

var writeEvent = context.CreateEvent();
context.WriteBuffer(bufferSlice, inputData, writeEvent);
context.Flush();

// Wait for write to complete before using the buffer
context.Wait(writeEvent);
context.DestroyEvent(writeEvent);
```
