xref: /aosp_15_r20/external/angle/src/libANGLE/cl_utils.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // cl_utils.cpp: Helper functions for the CL front end
7 
8 #include "libANGLE/cl_utils.h"
9 
10 #include "common/PackedCLEnums_autogen.h"
11 #include "libANGLE/renderer/CLExtensions.h"
12 
13 namespace cl
14 {
15 
GetChannelCount(cl_channel_order channelOrder)16 size_t GetChannelCount(cl_channel_order channelOrder)
17 {
18     size_t count = 0u;
19     switch (channelOrder)
20     {
21         case CL_R:
22         case CL_A:
23         case CL_LUMINANCE:
24         case CL_INTENSITY:
25         case CL_DEPTH:
26             count = 1u;
27             break;
28         case CL_RG:
29         case CL_RA:
30         case CL_Rx:
31             count = 2u;
32             break;
33         case CL_RGB:
34         case CL_RGx:
35         case CL_sRGB:
36             count = 3u;
37             break;
38         case CL_RGBA:
39         case CL_ARGB:
40         case CL_BGRA:
41         case CL_ABGR:
42         case CL_RGBx:
43         case CL_sRGBA:
44         case CL_sBGRA:
45         case CL_sRGBx:
46             count = 4u;
47             break;
48         default:
49             break;
50     }
51     return count;
52 }
53 
GetElementSize(const cl_image_format & image_format)54 size_t GetElementSize(const cl_image_format &image_format)
55 {
56     size_t size = 0u;
57     switch (image_format.image_channel_data_type)
58     {
59         case CL_SNORM_INT8:
60         case CL_UNORM_INT8:
61         case CL_SIGNED_INT8:
62         case CL_UNSIGNED_INT8:
63             size = GetChannelCount(image_format.image_channel_order);
64             break;
65         case CL_SNORM_INT16:
66         case CL_UNORM_INT16:
67         case CL_SIGNED_INT16:
68         case CL_UNSIGNED_INT16:
69         case CL_HALF_FLOAT:
70             size = 2u * GetChannelCount(image_format.image_channel_order);
71             break;
72         case CL_SIGNED_INT32:
73         case CL_UNSIGNED_INT32:
74         case CL_FLOAT:
75             size = 4u * GetChannelCount(image_format.image_channel_order);
76             break;
77         case CL_UNORM_SHORT_565:
78         case CL_UNORM_SHORT_555:
79             size = 2u;
80             break;
81         case CL_UNORM_INT_101010:
82         case CL_UNORM_INT_101010_2:
83             size = 4u;
84             break;
85         default:
86             break;
87     }
88     return size;
89 }
90 
IsValidImageFormat(const cl_image_format * imageFormat,const rx::CLExtensions & extensions)91 bool IsValidImageFormat(const cl_image_format *imageFormat, const rx::CLExtensions &extensions)
92 {
93     if (imageFormat == nullptr)
94     {
95         return false;
96     }
97     switch (imageFormat->image_channel_order)
98     {
99         case CL_R:
100         case CL_A:
101         case CL_LUMINANCE:
102         case CL_INTENSITY:
103         case CL_RG:
104         case CL_RA:
105         case CL_RGB:
106         case CL_RGBA:
107         case CL_ARGB:
108         case CL_BGRA:
109             break;
110 
111         case CL_Rx:
112         case CL_RGx:
113         case CL_RGBx:
114             if (extensions.version < CL_MAKE_VERSION(1, 1, 0))
115             {
116                 return false;
117             }
118             break;
119 
120         case CL_ABGR:
121         case CL_sRGB:
122         case CL_sRGBA:
123         case CL_sBGRA:
124         case CL_sRGBx:
125             if (extensions.version < CL_MAKE_VERSION(2, 0, 0))
126             {
127                 return false;
128             }
129             break;
130 
131         case CL_DEPTH:
132             // CL_DEPTH can only be used if channel data type = CL_UNORM_INT16 or CL_FLOAT.
133             if (imageFormat->image_channel_data_type != CL_UNORM_INT16 &&
134                 imageFormat->image_channel_data_type != CL_FLOAT)
135             {
136                 return false;
137             }
138             if (!extensions.khrDepthImages)
139             {
140                 return false;
141             }
142             break;
143 
144         default:
145             return false;
146     }
147     switch (imageFormat->image_channel_data_type)
148     {
149         case CL_SNORM_INT8:
150         case CL_SNORM_INT16:
151         case CL_UNORM_INT8:
152         case CL_UNORM_INT16:
153         case CL_SIGNED_INT8:
154         case CL_SIGNED_INT16:
155         case CL_SIGNED_INT32:
156         case CL_UNSIGNED_INT8:
157         case CL_UNSIGNED_INT16:
158         case CL_UNSIGNED_INT32:
159         case CL_HALF_FLOAT:
160         case CL_FLOAT:
161             break;
162 
163         case CL_UNORM_SHORT_565:
164         case CL_UNORM_SHORT_555:
165         case CL_UNORM_INT_101010:
166             if (imageFormat->image_channel_order != CL_RGB &&
167                 imageFormat->image_channel_order != CL_RGBx)
168             {
169                 return false;
170             }
171             break;
172 
173         case CL_UNORM_INT_101010_2:
174             if (extensions.version < CL_MAKE_VERSION(2, 1, 0) ||
175                 imageFormat->image_channel_order != CL_RGBA)
176             {
177                 return false;
178             }
179             break;
180 
181         default:
182             return false;
183     }
184     return true;
185 }
186 
IsImageType(cl::MemObjectType type)187 bool IsImageType(cl::MemObjectType type)
188 {
189     return (type >= cl::MemObjectType::Image2D && type <= cl::MemObjectType::Image1D_Buffer);
190 }
191 
IsBufferType(cl::MemObjectType type)192 bool IsBufferType(cl::MemObjectType type)
193 {
194     return type == cl::MemObjectType::Buffer;
195 }
196 
GetExtentFromDescriptor(cl::ImageDescriptor desc)197 cl::Extents GetExtentFromDescriptor(cl::ImageDescriptor desc)
198 {
199     cl::Extents extent{};
200 
201     extent.width  = desc.width;
202     extent.height = desc.height;
203     extent.depth  = desc.depth;
204 
205     // user can supply random values for height and depth for formats that dont need them
206     switch (desc.type)
207     {
208         case cl::MemObjectType::Image1D:
209         case cl::MemObjectType::Image1D_Array:
210         case cl::MemObjectType::Image1D_Buffer:
211             extent.height = 1;
212             extent.depth  = 1;
213             break;
214         case cl::MemObjectType::Image2D:
215         case cl::MemObjectType::Image2D_Array:
216             extent.depth = 1;
217             break;
218         default:
219             break;
220     }
221     return extent;
222 }
223 
224 thread_local cl_int gClErrorTls;
225 
226 }  // namespace cl
227