dmitri.shuralyov.com/gpu/mtl/...

cmd/mtlinfo: Add a tool to list all Metal devices, supported feature sets.

It might be desirable for some users to be able to run a command that
outputs information about Metal devices without having to write code.
This is similar to utilities such as glfwinfo, glewinfo, etc.
dmitshur committed 6 years ago commit 0cf138a823b3bcf750eace651e54f78d7eaa68ed
Collapse all
cmd/mtlinfo/main.go
@@ -0,0 +1,85 @@
// +build darwin

// mtlinfo is a tool that displays information about Metal devices in the system.
package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"dmitri.shuralyov.com/gpu/mtl"
)

func usage() {
	fmt.Fprintln(os.Stderr, "Usage: mtlinfo")
	flag.PrintDefaults()
}

func main() {
	flag.Usage = usage
	flag.Parse()

	err := run()
	if err != nil {
		log.Fatalln(err)
	}
}

func run() error {
	// Display the preferred system default Metal device.
	device, err := mtl.CreateSystemDefaultDevice()
	if err != nil {
		// An error here means Metal is not supported on this system.
		// Let the user know and stop here.
		fmt.Println(err)
		return nil
	}
	fmt.Println("preferred system default Metal device:", device.Name)

	// List all Metal devices in the system.
	allDevices := mtl.CopyAllDevices()
	for _, d := range allDevices {
		fmt.Println()
		printDeviceInfo(d)
	}

	return nil
}

func printDeviceInfo(d mtl.Device) {
	fmt.Println(d.Name + ":")
	fmt.Println("	• low-power:", yes(d.LowPower))
	fmt.Println("	• removable:", yes(d.Removable))
	fmt.Println("	• configured as headless:", yes(d.Headless))
	fmt.Println("	• registry ID:", d.RegistryID)
	fmt.Println()
	fmt.Println("	Feature Sets:")
	fmt.Println("	• macOS GPU family 1, version 1:", supported(d.SupportsFeatureSet(mtl.MacOSGPUFamily1V1)))
	fmt.Println("	• macOS GPU family 1, version 2:", supported(d.SupportsFeatureSet(mtl.MacOSGPUFamily1V2)))
	fmt.Println("	• macOS read-write texture, tier 2:", supported(d.SupportsFeatureSet(mtl.MacOSReadWriteTextureTier2)))
	fmt.Println("	• macOS GPU family 1, version 3:", supported(d.SupportsFeatureSet(mtl.MacOSGPUFamily1V3)))
	fmt.Println("	• macOS GPU family 1, version 4:", supported(d.SupportsFeatureSet(mtl.MacOSGPUFamily1V4)))
	fmt.Println("	• macOS GPU family 2, version 1:", supported(d.SupportsFeatureSet(mtl.MacOSGPUFamily2V1)))
}

func yes(v bool) string {
	switch v {
	case true:
		return "yes"
	case false:
		return "no"
	}
	panic("unreachable")
}

func supported(v bool) string {
	switch v {
	case true:
		return "✅ supported"
	case false:
		return "❌ unsupported"
	}
	panic("unreachable")
}