Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


WriteBuffer

Asynchronously copies data from host memory to a device buffer.
Read time 2 minutesLast updated 6 days ago

Definition

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

Asynchronously copies data from host memory to a device buffer.
void WriteBuffer<T>(BufferSlice<T> dst, NativeArray<T> src) where T : struct

Parameters

The BufferSlice specifying the destination on the device.

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() to ensure the operation begins executing, then use IDeviceContext.Wait or IDeviceContext.IsCompleted with the provided event to determine when the transfer is complete.
The source NativeArray<T> must remain allocated and unchanged until the write operation completes, as indicated by the event.
Warning: EventID instances are single-use. Do not reuse an event that has been passed to this method.

Examples

// Prepare data and write to bufferusing 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 buffercontext.Wait(writeEvent);context.DestroyEvent(writeEvent);

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

Asynchronously copies data from host memory to a device buffer.
void WriteBuffer<T>(BufferSlice<T> dst, NativeArray<T> src, EventID id) where T : struct

Parameters

The BufferSlice specifying the destination on the device.

The source array in CPU memory. Must remain valid until the operation completes.

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() to ensure the operation begins executing, then use IDeviceContext.Wait or IDeviceContext.IsCompleted with the provided event to determine when the transfer is complete.
The source NativeArray<T> must remain allocated and unchanged until the write operation completes, as indicated by the event.
Warning: EventID instances are single-use. Do not reuse an event that has been passed to this method.

Examples

// Prepare data and write to bufferusing 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 buffercontext.Wait(writeEvent);context.DestroyEvent(writeEvent);