Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


IDeviceContext

Provides an abstraction layer for memory management, synchronization, and compute operations across different devices.
Read time 2 minutesLast updated 7 days ago

Definition

public interface IDeviceContext : IDisposable

Remarks

The IDeviceContext interface abstracts memory, synchronization, and compute operations for different compute devices such as CPU and GPU. It enables implementation-agnostic code while providing access to device-specific optimizations.
Unity provides concrete implementations of this interface including:
The interface manages buffer allocation, asynchronous operations through events, and data transfer between host and device memory.

Examples

// Create and initialize a device contextusing var context = new RadeonRaysContext();bool initialized = context.Initialize();Assert.IsTrue(initialized, "Failed to initialize context");// Create a buffer and perform read/write operationsBufferID bufferId = context.CreateBuffer(1024, sizeof(int));var bufferSlice = new BufferSlice<int>(bufferId, 0);// Write data asynchronouslyusing var inputData = new NativeArray<int>(1024, Allocator.Persistent);for (int i = 0; i < inputData.Length; i++) inputData[i] = i;var writeEvent = context.CreateEvent();context.WriteBuffer(bufferSlice, inputData, writeEvent);// Read data backusing var outputData = new NativeArray<int>(1024, Allocator.Persistent);var readEvent = context.CreateEvent();context.ReadBuffer(bufferSlice, outputData, readEvent);// Flush and wait for completioncontext.Flush();context.Wait(readEvent);// Cleanupcontext.DestroyEvent(writeEvent);context.DestroyEvent(readEvent);context.DestroyBuffer(bufferId);

Methods

Method

Description

CreateBufferAllocates a new buffer on the device with the specified element count and stride.
CreateEventCreates a synchronization event for tracking asynchronous operations.
DestroyBufferDestroy the buffer with the given ID.
DestroyEventDestroy the event with the given ID. You should call this to free temporary resources associated with an event. Attempting to use the event after it has been destroyed, for example using IDeviceContext.Wait or IDeviceContext.IsCompleted will result in undefined behavior.
FlushInitiates execution of all enqueued operations on the device.
InitializeInitialize.
IsCompletedReturns true if the asynchronous operation completed.
ReadBufferAsynchronously copies data from a device buffer to host memory.
WaitWait for an asynchronous event.
WriteBufferAsynchronously copies data from host memory to a device buffer.

Inheritance

Inherited Members