xref: /aosp_15_r20/external/angle/src/libANGLE/cl_utils.h (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.h: Helper functions for the CL front end
7 
8 #ifndef LIBANGLE_CL_UTILS_H_
9 #define LIBANGLE_CL_UTILS_H_
10 
11 #include "common/PackedCLEnums_autogen.h"
12 #include "libANGLE/renderer/cl_types.h"
13 
14 #define ANGLE_CL_SET_ERROR(error) cl::gClErrorTls = error
15 
16 #define ANGLE_CL_RETURN_ERROR(error) \
17     do                               \
18     {                                \
19         cl::gClErrorTls = error;     \
20         return angle::Result::Stop;  \
21     } while (0)
22 
23 #define ANGLE_CL_TRY(expression)                           \
24     do                                                     \
25     {                                                      \
26         const cl_int ANGLE_LOCAL_VAR = expression;         \
27         if (ANGLE_UNLIKELY(ANGLE_LOCAL_VAR != CL_SUCCESS)) \
28         {                                                  \
29             ANGLE_CL_RETURN_ERROR(ANGLE_LOCAL_VAR);        \
30         }                                                  \
31     } while (0)
32 
33 #define ANGLE_CL_IMPL_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, (void))
34 #define ANGLE_CL_IMPL_TRY_ERROR(EXPR, ERROR) \
35     ANGLE_TRY_TEMPLATE(EXPR, ANGLE_CL_RETURN_ERROR(ERROR); (void))
36 
37 namespace cl
38 {
39 
40 size_t GetChannelCount(cl_channel_order channelOrder);
41 
42 size_t GetElementSize(const cl_image_format &image_format);
43 
OverlapRegions(size_t offset1,size_t offset2,size_t size)44 inline bool OverlapRegions(size_t offset1, size_t offset2, size_t size)
45 {
46     // From https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html
47     // The regions overlap if src_offset <= dst_offset <= src_offset + size - 1
48     // or if dst_offset <= src_offset <= dst_offset + size - 1.
49     return (offset1 <= offset2 && offset2 <= offset1 + size - 1u) ||
50            (offset2 <= offset1 && offset1 <= offset2 + size - 1u);
51 }
52 
53 bool IsValidImageFormat(const cl_image_format *imageFormat, const rx::CLExtensions &extensions);
54 
55 bool IsImageType(cl::MemObjectType memObjectType);
56 bool IsBufferType(cl::MemObjectType memObjectType);
57 
58 cl::Extents GetExtentFromDescriptor(cl::ImageDescriptor desc);
59 
60 extern thread_local cl_int gClErrorTls;
61 
62 }  // namespace cl
63 
64 #endif  // LIBANGLE_CL_UTILS_H_
65