Last active
November 23, 2022 19:22
-
-
Save jrprice/97b70932c072441f9e8500d7d7432604 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Metal; | |
int main(int argc, char *argv[]) { | |
NSString* path; | |
NSString* entry_point; | |
if (argc == 2) { | |
path = [NSString stringWithUTF8String:argv[1]]; | |
entry_point = nil; | |
} | |
else if (argc == 3) { | |
path = [NSString stringWithUTF8String:argv[1]]; | |
entry_point = [NSString stringWithUTF8String:argv[2]]; | |
} | |
else if (argc == 8) { | |
path = [NSString stringWithUTF8String:argv[7]]; | |
entry_point = nil; | |
} | |
else if (argc == 9) { | |
path = [NSString stringWithUTF8String:argv[7]]; | |
entry_point = [NSString stringWithUTF8String:argv[8]]; | |
} | |
else { | |
printf("Usage:\n"); | |
printf("\tmsl-compile FILE [ENTRY_POINT]\n"); | |
printf("\tmsl-compile '' '' '' '' '' '' FILE [ENTRY_POINT]\n"); | |
exit(1); | |
} | |
NSError* error = nil; | |
// Load source. | |
NSString* source = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; | |
if(!source) { | |
NSString *output = [error localizedDescription]; | |
printf("Failed to load source: %s\n", [output UTF8String]); | |
return 1; | |
} | |
// Build MSL library. | |
id<MTLDevice> device = MTLCopyAllDevices()[0]; | |
printf("Device = %s\n", [device.name UTF8String]); | |
id<MTLLibrary> library = [device newLibraryWithSource:source options:nil error:&error]; | |
if(!library) { | |
NSString *output = [error localizedDescription]; | |
printf("Library creation failed: %s\n", [output UTF8String]); | |
return 1; | |
} else { | |
printf("Library created successfully.\n"); | |
} | |
if (entry_point) { | |
// TODO: Assumes compute entry point | |
// Create a shader function. | |
id<MTLFunction> function = [library newFunctionWithName:entry_point]; | |
if(!function) { | |
printf("Failed to make entry point\n"); | |
return 1; | |
} | |
// Build compute pipeline state. | |
id<MTLComputePipelineState> pipeline = | |
[device newComputePipelineStateWithFunction:function error:&error]; | |
if (!pipeline) { | |
NSString *output = [error localizedDescription]; | |
printf("Pipeline creation failed: %s\n", [output UTF8String]); | |
return 1; | |
} else { | |
printf("Pipeline created successfully.\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment