1// Copyright 2021 The Tint Authors. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#ifdef TINT_ENABLE_MSL_COMPILATION_USING_METAL_API 16 17@import Metal; 18 19// Disable: error: treating #include as an import of module 'std.string' 20#pragma clang diagnostic push 21#pragma clang diagnostic ignored "-Wauto-import" 22#include "compile.h" 23#pragma clang diagnostic pop 24 25CompileResult CompileMslUsingMetalAPI(const std::string& src) { 26 CompileResult result; 27 result.success = false; 28 29 NSError* error = nil; 30 31 id<MTLDevice> device = MTLCreateSystemDefaultDevice(); 32 if (!device) { 33 result.output = "MTLCreateSystemDefaultDevice returned null"; 34 result.success = false; 35 return result; 36 } 37 38 NSString* source = [NSString stringWithCString:src.c_str() 39 encoding:NSUTF8StringEncoding]; 40 41 MTLCompileOptions* compileOptions = [MTLCompileOptions new]; 42 compileOptions.languageVersion = MTLLanguageVersion1_2; 43 44 id<MTLLibrary> library = [device newLibraryWithSource:source 45 options:compileOptions 46 error:&error]; 47 if (!library) { 48 NSString* output = [error localizedDescription]; 49 result.output = [output UTF8String]; 50 result.success = false; 51 } 52 53 return result; 54} 55 56#endif 57