xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/metal/common.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 <map>
22 #include <utility>
23 
24 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
25 #include "tensorflow/lite/delegates/gpu/common/status.h"
26 
27 namespace tflite {
28 namespace gpu {
29 namespace metal {
30 
31 /// Returns system default device on iOS or Intel GPU on macOS.
32 id<MTLDevice> GetBestSupportedMetalDevice();
33 
34 absl::Status CreateComputeProgram(
35     id<MTLDevice> device, const std::string& code,
36     const std::string& function_name,
37     const std::map<std::string, std::string>& macros,
38     id<MTLComputePipelineState>* program);
39 
40 absl::Status CreateComputeProgramWithArgumentBuffer(
41     id<MTLDevice> device, const std::string& code,
42     const std::string& function_name,
43     const std::map<std::string, std::string>& macros,
44     id<MTLComputePipelineState>* program,
45     id<MTLArgumentEncoder>* arguments_encoder);
46 
47 // ICB - indirect command buffer
48 absl::Status CreateComputeProgramWithICBSupport(
49     id<MTLDevice> device, const std::string& code,
50     const std::string& function_name,
51     const std::map<std::string, std::string>& macros,
52     id<MTLComputePipelineState>* program,
53     id<MTLArgumentEncoder>* arguments_encoder);
54 
55 absl::Status CreateFunction(id<MTLDevice> device, const std::string& code,
56                             const std::string& function_name,
57                             const std::map<std::string, std::string>& macros,
58                             id<MTLFunction>* function);
59 
60 int PixelFormatToSizeInBytes(MTLPixelFormat pixel_format);
61 MTLPixelFormat DataTypeToRGBAPixelFormat(DataType type, bool normalized = false);
62 
63 void WriteDataToTexture2D(id<MTLTexture> texture, id<MTLDevice> device, const void* data);
64 void ReadDataFromTexture2D(id<MTLTexture> texture, id<MTLDevice> device, void* data);
65 
66 void WriteDataToTexture3D(id<MTLTexture> texture, id<MTLDevice> device, const void* data);
67 void ReadDataFromTexture3D(id<MTLTexture> texture, id<MTLDevice> device, void* data);
68 
69 void WriteDataToTexture2DArray(id<MTLTexture> texture, id<MTLDevice> device, const void* data);
70 void ReadDataFromTexture2DArray(id<MTLTexture> texture, id<MTLDevice> device, void* data);
71 
72 }  // namespace metal
73 }  // namespace gpu
74 }  // namespace tflite
75 
76 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMMON_H_
77