1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 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 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMMON_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMMON_H_ 18 19 #import <Metal/Metal.h> 20 21 #include <utility> 22 23 #include "tensorflow/lite/delegates/gpu/common/data_type.h" 24 #include "tensorflow/lite/delegates/gpu/common/status.h" 25 26 namespace tflite { 27 namespace gpu { 28 namespace metal { 29 30 /// Returns system default device on iOS or Intel GPU on macOS. 31 id<MTLDevice> GetBestSupportedMetalDevice(); 32 33 /// Metal compute shader compilation 34 /// @param device The device on which that shader program will be stored. 35 /// @param code Shader source. 36 /// @param functionName The name of the main shader function. 37 /// @param macros Compile-time definitions. 38 /// @param program A non-nil pointer to the program object that will be filled. 39 /// @return Returns a valid program pointer or error string. At least one pointer is valid but not 40 /// both. 41 /// @discussion The function autoselects the maximum shader language version supported by the target 42 /// OS. FastMath is enabled. 43 absl::Status CreateComputeProgram(id<MTLDevice> device, NSString* code, NSString* functionName, 44 NSDictionary<NSString*, NSString*>* macros, 45 id<MTLComputePipelineState>* program); 46 47 int PixelFormatToSizeInBytes(MTLPixelFormat pixel_format); 48 MTLPixelFormat DataTypeToRGBAPixelFormat(DataType type, bool normalized = false); 49 50 void WriteDataToTexture2D(id<MTLTexture> texture, id<MTLDevice> device, const void* data); 51 void ReadDataFromTexture2D(id<MTLTexture> texture, id<MTLDevice> device, void* data); 52 53 void WriteDataToTexture3D(id<MTLTexture> texture, id<MTLDevice> device, const void* data); 54 void ReadDataFromTexture3D(id<MTLTexture> texture, id<MTLDevice> device, void* data); 55 56 void WriteDataToTexture2DArray(id<MTLTexture> texture, id<MTLDevice> device, const void* data); 57 void ReadDataFromTexture2DArray(id<MTLTexture> texture, id<MTLDevice> device, void* data); 58 59 } // namespace metal 60 } // namespace gpu 61 } // namespace tflite 62 63 #endif // TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMMON_H_ 64