|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Text; |
| 4 | +using LevelZero.Native; |
| 5 | + |
| 6 | +namespace LevelZero; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Represents a Level Zero compute device with its context, command queue, and command list. |
| 10 | +/// This is the primary object for GPU interaction — load modules, allocate memory, and launch kernels. |
| 11 | +/// </summary> |
| 12 | +public sealed class ComputeDevice : IDisposable |
| 13 | +{ |
| 14 | + private IntPtr _driver; |
| 15 | + private IntPtr _device; |
| 16 | + private IntPtr _context; |
| 17 | + private IntPtr _queue; |
| 18 | + private IntPtr _commandList; |
| 19 | + private bool _disposed; |
| 20 | + |
| 21 | + /// <summary>Human-readable device name (e.g. "Intel(R) UHD Graphics 770").</summary> |
| 22 | + public string Name { get; } |
| 23 | + |
| 24 | + /// <summary>Driver index this device belongs to.</summary> |
| 25 | + public uint DriverIndex { get; } |
| 26 | + |
| 27 | + /// <summary>Device index within the driver.</summary> |
| 28 | + public uint DeviceIndex { get; } |
| 29 | + |
| 30 | + internal IntPtr ContextHandle => !_disposed ? _context : throw new ObjectDisposedException(nameof(ComputeDevice)); |
| 31 | + internal IntPtr DeviceHandle => !_disposed ? _device : throw new ObjectDisposedException(nameof(ComputeDevice)); |
| 32 | + |
| 33 | + private ComputeDevice(IntPtr driver, IntPtr device, IntPtr context, IntPtr queue, IntPtr commandList, |
| 34 | + string name, uint driverIndex, uint deviceIndex) |
| 35 | + { |
| 36 | + _driver = driver; |
| 37 | + _device = device; |
| 38 | + _context = context; |
| 39 | + _queue = queue; |
| 40 | + _commandList = commandList; |
| 41 | + Name = name; |
| 42 | + DriverIndex = driverIndex; |
| 43 | + DeviceIndex = deviceIndex; |
| 44 | + } |
| 45 | + |
| 46 | + internal static ComputeDevice Create(uint driverIndex, uint deviceIndex) |
| 47 | + { |
| 48 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_init(0)); |
| 49 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_get_driver_handle(driverIndex, out var driver)); |
| 50 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_get_device_handle(driverIndex, deviceIndex, out var device)); |
| 51 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_context_create(driver, out var context)); |
| 52 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_command_queue_create(context, device, out var queue)); |
| 53 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_command_list_create(context, device, out var commandList)); |
| 54 | + |
| 55 | + var nameBuf = new StringBuilder(256); |
| 56 | + LevelZeroNative.lz_get_device_name(driverIndex, deviceIndex, nameBuf, (uint)nameBuf.Capacity); |
| 57 | + var name = nameBuf.ToString(); |
| 58 | + |
| 59 | + return new ComputeDevice(driver, device, context, queue, commandList, name, driverIndex, deviceIndex); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Loads a SPIR-V module from a file path. |
| 64 | + /// </summary> |
| 65 | + public ComputeModule LoadModule(string spirvPath) |
| 66 | + { |
| 67 | + ObjectDisposedException.ThrowIf(_disposed, this); |
| 68 | + var spirv = File.ReadAllBytes(spirvPath); |
| 69 | + return LoadModule(spirv); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Loads a SPIR-V module from a byte array. |
| 74 | + /// </summary> |
| 75 | + public ComputeModule LoadModule(byte[] spirv) |
| 76 | + { |
| 77 | + ObjectDisposedException.ThrowIf(_disposed, this); |
| 78 | + var logBuf = new StringBuilder(4096); |
| 79 | + var result = LevelZeroNative.lz_module_create(_context, _device, spirv, (uint)spirv.Length, |
| 80 | + out var module, logBuf, (uint)logBuf.Capacity); |
| 81 | + var buildLog = logBuf.ToString(); |
| 82 | + if (result != 0) |
| 83 | + throw new LevelZeroException(result, |
| 84 | + $"Module compilation failed. Build log: {buildLog}"); |
| 85 | + return new ComputeModule(module, _context, buildLog); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Tries to load a SPIR-V module. Returns null on failure instead of throwing. |
| 90 | + /// </summary> |
| 91 | + public ComputeModule? TryLoadModule(string spirvPath, out string buildLog) |
| 92 | + { |
| 93 | + buildLog = string.Empty; |
| 94 | + if (string.IsNullOrWhiteSpace(spirvPath) || !File.Exists(spirvPath)) |
| 95 | + return null; |
| 96 | + |
| 97 | + var spirv = File.ReadAllBytes(spirvPath); |
| 98 | + return TryLoadModule(spirv, out buildLog); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Tries to load a SPIR-V module from bytes. Returns null on failure. |
| 103 | + /// </summary> |
| 104 | + public ComputeModule? TryLoadModule(byte[] spirv, out string buildLog) |
| 105 | + { |
| 106 | + ObjectDisposedException.ThrowIf(_disposed, this); |
| 107 | + var logBuf = new StringBuilder(4096); |
| 108 | + var result = LevelZeroNative.lz_module_create(_context, _device, spirv, (uint)spirv.Length, |
| 109 | + out var module, logBuf, (uint)logBuf.Capacity); |
| 110 | + buildLog = logBuf.ToString(); |
| 111 | + return result == 0 ? new ComputeModule(module, _context, buildLog) : null; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Allocates a typed USM shared-memory buffer accessible from both host and device. |
| 116 | + /// </summary> |
| 117 | + public SharedBuffer<T> AllocShared<T>(int count) where T : unmanaged |
| 118 | + { |
| 119 | + ObjectDisposedException.ThrowIf(_disposed, this); |
| 120 | + var byteSize = (UIntPtr)(count * Unsafe.SizeOf<T>()); |
| 121 | + LevelZeroNative.EnsureSuccess( |
| 122 | + LevelZeroNative.lz_usm_alloc_shared(_context, _device, byteSize, UIntPtr.Zero, out var ptr)); |
| 123 | + return new SharedBuffer<T>(_context, ptr, count); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Allocates a shared buffer and immediately fills it from a source array. |
| 128 | + /// </summary> |
| 129 | + public SharedBuffer<T> AllocShared<T>(T[] data) where T : unmanaged |
| 130 | + { |
| 131 | + var buffer = AllocShared<T>(data.Length); |
| 132 | + buffer.Write(data); |
| 133 | + return buffer; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Launches a kernel with the given work-group counts across up to 3 dimensions, |
| 138 | + /// then synchronizes (blocks until complete). |
| 139 | + /// </summary> |
| 140 | + public void Launch(ComputeKernel kernel, uint groupCountX, uint groupCountY = 1, uint groupCountZ = 1) |
| 141 | + { |
| 142 | + ObjectDisposedException.ThrowIf(_disposed, this); |
| 143 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_command_list_reset(_commandList)); |
| 144 | + LevelZeroNative.EnsureSuccess( |
| 145 | + LevelZeroNative.lz_command_list_append_launch_kernel(_commandList, kernel.Handle, |
| 146 | + groupCountX, groupCountY, groupCountZ)); |
| 147 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_command_list_close(_commandList)); |
| 148 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_command_queue_execute(_queue, _commandList)); |
| 149 | + LevelZeroNative.EnsureSuccess(LevelZeroNative.lz_command_queue_synchronize(_queue, ulong.MaxValue)); |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary>Computes the number of work-groups needed to cover totalItems with the given local size.</summary> |
| 153 | + public static uint GroupCount(int totalItems, uint localSize) => |
| 154 | + (uint)Math.Max(1, (totalItems + (int)localSize - 1) / (int)localSize); |
| 155 | + |
| 156 | + public void Dispose() |
| 157 | + { |
| 158 | + if (_disposed) return; |
| 159 | + _disposed = true; |
| 160 | + |
| 161 | + if (_commandList != IntPtr.Zero) |
| 162 | + LevelZeroNative.lz_command_list_destroy(_commandList); |
| 163 | + if (_queue != IntPtr.Zero) |
| 164 | + LevelZeroNative.lz_command_queue_destroy(_queue); |
| 165 | + if (_context != IntPtr.Zero) |
| 166 | + LevelZeroNative.lz_context_destroy(_context); |
| 167 | + |
| 168 | + _commandList = _queue = _context = _device = _driver = IntPtr.Zero; |
| 169 | + } |
| 170 | +} |
0 commit comments