# IDeviceContext

> Provides an abstraction layer for memory management, synchronization, and compute operations across different devices.

## Definition

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

```csharp
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:

* [RadeonRaysContext](/engine/6000.6/script-reference/unityengine/lighttransport/radeonrayscontext.md) for GPU-accelerated operations using OpenCL 1.2.

The interface manages buffer allocation, asynchronous operations through events, and data transfer between host and device memory.

## Examples

```csharp
// Create and initialize a device context
using var context = new RadeonRaysContext();
bool initialized = context.Initialize();
Assert.IsTrue(initialized, "Failed to initialize context");

// Create a buffer and perform read/write operations
BufferID bufferId = context.CreateBuffer(1024, sizeof(int));
var bufferSlice = new BufferSlice<int>(bufferId, 0);

// Write data asynchronously
using 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 back
using var outputData = new NativeArray<int>(1024, Allocator.Persistent);
var readEvent = context.CreateEvent();
context.ReadBuffer(bufferSlice, outputData, readEvent);

// Flush and wait for completion
context.Flush();
context.Wait(readEvent);

// Cleanup
context.DestroyEvent(writeEvent);
context.DestroyEvent(readEvent);
context.DestroyBuffer(bufferId);
```

## Methods

| Method                                                                                                      | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [CreateBuffer](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/createbuffer.md)   | Allocates a new buffer on the device with the specified element count and stride.                                                                                                                                                                                                                                                                                                                                                                                |
| [CreateEvent](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/createevent.md)     | Creates a synchronization event for tracking asynchronous operations.                                                                                                                                                                                                                                                                                                                                                                                            |
| [DestroyBuffer](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/destroybuffer.md) | Destroy the buffer with the given ID.                                                                                                                                                                                                                                                                                                                                                                                                                            |
| [DestroyEvent](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/destroyevent.md)   | Destroy 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](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/wait.md) or [IDeviceContext.IsCompleted](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/iscompleted.md) will result in undefined behavior. |
| [Flush](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/flush.md)                 | Initiates execution of all enqueued operations on the device.                                                                                                                                                                                                                                                                                                                                                                                                    |
| [Initialize](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/initialize.md)       | Initialize.                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| [IsCompleted](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/iscompleted.md)     | Returns true if the asynchronous operation completed.                                                                                                                                                                                                                                                                                                                                                                                                            |
| [ReadBuffer](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/readbuffer.md)       | Asynchronously copies data from a device buffer to host memory.                                                                                                                                                                                                                                                                                                                                                                                                  |
| [Wait](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/wait.md)                   | Wait for an asynchronous event.                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| [WriteBuffer](/engine/6000.6/script-reference/unityengine/lighttransport/idevicecontext/writebuffer.md)     | Asynchronously copies data from host memory to a device buffer.                                                                                                                                                                                                                                                                                                                                                                                                  |

## Inheritance

**Inherited Members:**

* [IDisposable.Dispose()](https://learn.microsoft.com/dotnet/api/system.idisposable.dispose)
