util_metal.h (3698B)
1 /* 2 Copyright 2010-2011, D. E. Shaw Research. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions, and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions, and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 * Neither the name of D. E. Shaw Research nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 #ifndef UTIL_METAL_H__ 33 #define UTIL_METAL_H__ 34 /* 35 * has a couple of utility functions to setup and teardown Metal. 36 * Avoid much boilerplate in every Metal program 37 * 38 * Written by Tom Schoonjans <Tom.Schoonjans@me.com> 39 */ 40 41 #import <Foundation/Foundation.h> 42 #import <Metal/Metal.h> 43 44 #include "util.h" 45 46 typedef struct umetal_info { 47 id<MTLDevice> device; 48 id<MTLCommandQueue> queue; 49 id<MTLLibrary> library; 50 } UMetalInfo; 51 52 /* Miscellaneous checking macros for convenience */ 53 static char *print_metal_errstring(NSError *err) { 54 if (err == nil) 55 return nil; 56 return strdup(err.localizedDescription.UTF8String); 57 } 58 59 #define CHECKERR(x) do { \ 60 (x); \ 61 if (err != nil) { \ 62 fprintf(stderr, "%s: error %s from %s\n", progname, print_metal_errstring(err), #x); \ 63 exit(1); \ 64 } \ 65 } while(0) 66 67 //#define CHECK(x) CHECKERR(err = (x)) 68 69 static UMetalInfo *metal_init(const char *devstr, const char *metallib) 70 { 71 UMetalInfo *tp; 72 NSError *err = nil; 73 NSArray<id<MTLDevice>> *devices; 74 unsigned i; 75 unsigned long ndevices; 76 77 /* get list of platforms */ 78 CHECKNOTZERO(devices = MTLCopyAllDevices()); 79 ndevices = (unsigned long) devices.count; 80 dprintf(("ndevices = %lu\n", ndevices)); 81 if (ndevices == 0) { 82 fprintf(stderr, "No Metal devices available\n"); 83 return NULL; 84 } 85 dprintf(("found %lu device%s\n", ndevices, ndevices == 1 ? "" : "s")); 86 CHECKNOTZERO(tp = (UMetalInfo *) malloc(sizeof(UMetalInfo))); 87 for (i = 0; i < devices.count; i++) { 88 dprintf(("device %d: %s\n", i, devices[i].name.UTF8String)); 89 } 90 91 // get default device 92 tp->device = MTLCreateSystemDefaultDevice(); 93 94 dprintf(("create Metal command queue for device %s\n", tp->device.name.UTF8String)); 95 CHECKNOTZERO(tp->queue = [tp->device newCommandQueue]); 96 dprintf(("create Metal library from %s\n", metallib)); 97 CHECKERR(tp->library = [tp->device newLibraryWithFile: [[NSString alloc] initWithUTF8String:metallib] error:&err]); 98 99 return tp; 100 } 101 102 103 static void metal_done(UMetalInfo *tp) { 104 105 dprintf(("metal_done\n")); 106 [tp->library release]; 107 [tp->queue release]; 108 [tp->device release]; 109 free(tp); 110 } 111 112 113 #endif /* UTIL_METAL_H__ */