Skip to content

Commit

Permalink
return error when no Metal support
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitshur committed Jul 18, 2019
1 parent ea8a996 commit 3dd3c38
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
14 changes: 11 additions & 3 deletions 246/mtl/mtl.go
@@ -1,14 +1,18 @@
// Package mtl is a tiny subset of the Metal API. // Package mtl is a tiny subset of the Metal API.
package mtl package mtl


import (
"errors"
"unsafe"
)

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


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


// CreateSystemDefaultDevice returns the preferred system default Metal device. // CreateSystemDefaultDevice returns the preferred system default Metal device.
func CreateSystemDefaultDevice() Device { func CreateSystemDefaultDevice() (Device, error) {
d := C.CreateSystemDefaultDevice() 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{ return Device{
Headless: d.headless != 0, Headless: d.headless != 0,
LowPower: d.lowPower != 0, LowPower: d.lowPower != 0,
Removable: d.removable != 0, Removable: d.removable != 0,
RegistryID: uint64(d.registryID), RegistryID: uint64(d.registryID),
Name: C.GoString(d.name), Name: C.GoString(d.name),
} }, nil
} }


// CopyAllDevices returns all Metal devices in the system. // CopyAllDevices returns all Metal devices in the system.
Expand Down
2 changes: 1 addition & 1 deletion 246/mtl/mtl.h
Expand Up @@ -14,5 +14,5 @@ struct Devices {
int length; int length;
}; };


struct Device CreateSystemDefaultDevice(); struct Device * CreateSystemDefaultDevice();
struct Devices CopyAllDevices(); struct Devices CopyAllDevices();
18 changes: 11 additions & 7 deletions 246/mtl/mtl.m
Expand Up @@ -2,15 +2,19 @@
#import <Metal/Metal.h> #import <Metal/Metal.h>
#include "mtl.h" #include "mtl.h"


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


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


Expand Down

0 comments on commit 3dd3c38

Please sign in to comment.