iOS: Fix crash on device

This commit is contained in:
yonihemi
2025-01-06 22:01:41 +08:00
parent 6181487bad
commit 63a47d0ba5
2 changed files with 27 additions and 12 deletions

View File

@ -209,9 +209,13 @@ pub const GPUState = struct {
} }
fn chooseDevice() error{NoMetalDevice}!objc.Object { fn chooseDevice() error{NoMetalDevice}!objc.Object {
var chosen_device: ?objc.Object = null;
switch (comptime builtin.os.tag) {
.macos => {
const devices = objc.Object.fromId(mtl.MTLCopyAllDevices()); const devices = objc.Object.fromId(mtl.MTLCopyAllDevices());
defer devices.release(); defer devices.release();
var chosen_device: ?objc.Object = null;
var iter = devices.iterate(); var iter = devices.iterate();
while (iter.next()) |device| { while (iter.next()) |device| {
// We want a GPU thats connected to a display. // We want a GPU thats connected to a display.
@ -223,6 +227,13 @@ pub const GPUState = struct {
if (device.getProperty(bool, "isRemovable") or if (device.getProperty(bool, "isRemovable") or
device.getProperty(bool, "isLowPower")) break; device.getProperty(bool, "isLowPower")) break;
} }
},
.ios => {
chosen_device = objc.Object.fromId(mtl.MTLCreateSystemDefaultDevice());
},
else => @compileError("unsupported target for Metal"),
}
const device = chosen_device orelse return error.NoMetalDevice; const device = chosen_device orelse return error.NoMetalDevice;
return device.retain(); return device.retain();
} }

View File

@ -175,4 +175,8 @@ pub const MTLSize = extern struct {
depth: c_ulong, depth: c_ulong,
}; };
/// https://developer.apple.com/documentation/metal/1433367-mtlcopyalldevices
pub extern "c" fn MTLCopyAllDevices() ?*anyopaque; pub extern "c" fn MTLCopyAllDevices() ?*anyopaque;
/// https://developer.apple.com/documentation/metal/1433401-mtlcreatesystemdefaultdevice
pub extern "c" fn MTLCreateSystemDefaultDevice() ?*anyopaque;