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 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h"
17
18 #include "absl/strings/str_cat.h"
19 #include "tensorflow/lite/delegates/gpu/cl/cl_image_format.h"
20 #include "tensorflow/lite/delegates/gpu/cl/util.h"
21 #include "tensorflow/lite/delegates/gpu/common/status.h"
22
23 namespace tflite {
24 namespace gpu {
25 namespace cl {
26 namespace {
27
GetSupportedImage2DFormats(cl_context context,cl_mem_flags flags)28 std::vector<cl_image_format> GetSupportedImage2DFormats(cl_context context,
29 cl_mem_flags flags) {
30 cl_uint num_image_formats;
31 cl_int error = clGetSupportedImageFormats(
32 context, flags, CL_MEM_OBJECT_IMAGE2D, 0, nullptr, &num_image_formats);
33 if (error != CL_SUCCESS) {
34 return {};
35 }
36
37 std::vector<cl_image_format> result(num_image_formats);
38 error = clGetSupportedImageFormats(context, flags, CL_MEM_OBJECT_IMAGE2D,
39 num_image_formats, &result[0], nullptr);
40 if (error != CL_SUCCESS) {
41 return {};
42 }
43 return result;
44 }
45
IsEqualToImageFormat(cl_image_format image_format,DataType data_type,int num_channels)46 bool IsEqualToImageFormat(cl_image_format image_format, DataType data_type,
47 int num_channels) {
48 return image_format.image_channel_data_type ==
49 ToImageChannelType(data_type) &&
50 image_format.image_channel_order == ToChannelOrder(num_channels);
51 }
52
AddSupportedImageFormats(cl_context context,GpuInfo * info)53 void AddSupportedImageFormats(cl_context context, GpuInfo* info) {
54 auto supported_formats =
55 GetSupportedImage2DFormats(context, CL_MEM_READ_WRITE);
56 for (auto format : supported_formats) {
57 info->opencl_info.supports_r_f16_tex2d =
58 info->opencl_info.supports_r_f16_tex2d ||
59 IsEqualToImageFormat(format, DataType::FLOAT16, 1);
60 info->opencl_info.supports_rg_f16_tex2d =
61 info->opencl_info.supports_rg_f16_tex2d ||
62 IsEqualToImageFormat(format, DataType::FLOAT16, 2);
63 info->opencl_info.supports_rgb_f16_tex2d =
64 info->opencl_info.supports_rgb_f16_tex2d ||
65 IsEqualToImageFormat(format, DataType::FLOAT16, 3);
66 info->opencl_info.supports_rgba_f16_tex2d =
67 info->opencl_info.supports_rgba_f16_tex2d ||
68 IsEqualToImageFormat(format, DataType::FLOAT16, 4);
69 info->opencl_info.supports_r_f32_tex2d =
70 info->opencl_info.supports_r_f32_tex2d ||
71 IsEqualToImageFormat(format, DataType::FLOAT32, 1);
72 info->opencl_info.supports_rg_f32_tex2d =
73 info->opencl_info.supports_rg_f32_tex2d ||
74 IsEqualToImageFormat(format, DataType::FLOAT32, 2);
75 info->opencl_info.supports_rgb_f32_tex2d =
76 info->opencl_info.supports_rgb_f32_tex2d ||
77 IsEqualToImageFormat(format, DataType::FLOAT32, 3);
78 info->opencl_info.supports_rgba_f32_tex2d =
79 info->opencl_info.supports_rgba_f32_tex2d ||
80 IsEqualToImageFormat(format, DataType::FLOAT32, 4);
81 }
82 }
83
CreateCLContext(const CLDevice & device,cl_context_properties * properties,CLContext * result)84 absl::Status CreateCLContext(const CLDevice& device,
85 cl_context_properties* properties,
86 CLContext* result) {
87 int error_code;
88 cl_device_id device_id = device.id();
89 cl_context context =
90 clCreateContext(properties, 1, &device_id, nullptr, nullptr, &error_code);
91 if (!context) {
92 return absl::UnknownError(
93 absl::StrCat("Failed to create a compute context - ",
94 CLErrorCodeToString(error_code)));
95 }
96 AddSupportedImageFormats(context, &device.info_);
97
98 *result = CLContext(context, true);
99 return absl::OkStatus();
100 }
101
102 } // namespace
103
CLContext(cl_context context,bool has_ownership)104 CLContext::CLContext(cl_context context, bool has_ownership)
105 : context_(context), has_ownership_(has_ownership) {}
106
CLContext(CLContext && context)107 CLContext::CLContext(CLContext&& context)
108 : context_(context.context_), has_ownership_(context.has_ownership_) {
109 context.context_ = nullptr;
110 }
111
operator =(CLContext && context)112 CLContext& CLContext::operator=(CLContext&& context) {
113 if (this != &context) {
114 Release();
115 std::swap(context_, context.context_);
116 has_ownership_ = context.has_ownership_;
117 }
118 return *this;
119 }
120
~CLContext()121 CLContext::~CLContext() { Release(); }
122
Release()123 void CLContext::Release() {
124 if (has_ownership_ && context_) {
125 clReleaseContext(context_);
126 context_ = nullptr;
127 }
128 }
129
IsFloatTexture2DSupported(int num_channels,DataType data_type,cl_mem_flags flags) const130 bool CLContext::IsFloatTexture2DSupported(int num_channels, DataType data_type,
131 cl_mem_flags flags) const {
132 auto supported_formats = GetSupportedImage2DFormats(context_, flags);
133 for (auto format : supported_formats) {
134 if (format.image_channel_data_type == ToImageChannelType(data_type) &&
135 format.image_channel_order == ToChannelOrder(num_channels)) {
136 return true;
137 }
138 }
139
140 return false;
141 }
142
CreateCLContext(const CLDevice & device,CLContext * result)143 absl::Status CreateCLContext(const CLDevice& device, CLContext* result) {
144 return CreateCLContext(device, nullptr, result);
145 }
146
CreateCLGLContext(const CLDevice & device,cl_context_properties egl_context,cl_context_properties egl_display,CLContext * result)147 absl::Status CreateCLGLContext(const CLDevice& device,
148 cl_context_properties egl_context,
149 cl_context_properties egl_display,
150 CLContext* result) {
151 if (!device.GetInfo().SupportsExtension("cl_khr_gl_sharing")) {
152 return absl::UnavailableError("Device doesn't support CL-GL sharing.");
153 }
154 cl_context_properties platform =
155 reinterpret_cast<cl_context_properties>(device.platform());
156 cl_context_properties props[] = {CL_GL_CONTEXT_KHR,
157 egl_context,
158 CL_EGL_DISPLAY_KHR,
159 egl_display,
160 CL_CONTEXT_PLATFORM,
161 platform,
162 0};
163 return CreateCLContext(device, props, result);
164 }
165
166 } // namespace cl
167 } // namespace gpu
168 } // namespace tflite
169