Skip to content
Permalink
Browse files

return error when no Metal support

  • Loading branch information...
dmitshur committed Jun 6, 2018
1 parent ea8a996 commit 3dd3c381ccfc6df14d52cef1dcbc8b5008241ee2
Showing with 23 additions and 11 deletions.
  1. +11 −3 246/mtl/mtl.go
  2. +1 −1 246/mtl/mtl.h
  3. +11 −7 246/mtl/mtl.m
@@ -1,14 +1,18 @@
// Package mtl is a tiny subset of the Metal API.
package mtl

import (
"errors"
"unsafe"
)

/*
#cgo darwin CFLAGS: -x objective-c
#cgo darwin LDFLAGS: -framework Metal
#include <stdlib.h>
#include "mtl.h"
*/
import "C"
import "unsafe"

// Device is abstract representation of the GPU that
// serves as the primary interface for a Metal app.
@@ -30,16 +34,20 @@ type Device struct {
}

// CreateSystemDefaultDevice returns the preferred system default Metal device.
func CreateSystemDefaultDevice() Device {
func CreateSystemDefaultDevice() (Device, error) {
d := C.CreateSystemDefaultDevice()
if d == nil {
return Device{}, errors.New("Metal is not supported on this system")
}
defer C.free(unsafe.Pointer(d))

return Device{
Headless: d.headless != 0,
LowPower: d.lowPower != 0,
Removable: d.removable != 0,
RegistryID: uint64(d.registryID),
Name: C.GoString(d.name),
}
}, nil
}

// CopyAllDevices returns all Metal devices in the system.
@@ -14,5 +14,5 @@ struct Devices {
int length;
};

struct Device CreateSystemDefaultDevice();
struct Device * CreateSystemDefaultDevice();
struct Devices CopyAllDevices();
@@ -2,15 +2,19 @@
#import <Metal/Metal.h>
#include "mtl.h"

struct Device CreateSystemDefaultDevice() {
// Caller must call free(d).
struct Device * CreateSystemDefaultDevice() {
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if (!device) {
return NULL;
}

struct Device d;
d.headless = device.headless;
d.lowPower = device.lowPower;
d.removable = device.removable;
d.registryID = device.registryID;
d.name = device.name.UTF8String;
struct Device * d = malloc(sizeof(struct Device));
d->headless = device.headless;
d->lowPower = device.lowPower;
d->removable = device.removable;
d->registryID = device.registryID;
d->name = device.name.UTF8String;
return d;
}

0 comments on commit 3dd3c38

Please sign in to comment.
You can’t perform that action at this time.