• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libANGLE/renderer/cl_types.h"
12 
13 #define ANGLE_CL_SET_ERROR(error) cl::gClErrorTls = error
14 
15 #define ANGLE_CL_RETURN_ERROR(error) \
16     do                               \
17     {                                \
18         cl::gClErrorTls = error;     \
19         return angle::Result::Stop;  \
20     } while (0)
21 
22 #define ANGLE_CL_TRY(expression)                           \
23     do                                                     \
24     {                                                      \
25         const cl_int ANGLE_LOCAL_VAR = expression;         \
26         if (ANGLE_UNLIKELY(ANGLE_LOCAL_VAR != CL_SUCCESS)) \
27         {                                                  \
28             ANGLE_CL_RETURN_ERROR(ANGLE_LOCAL_VAR);        \
29         }                                                  \
30     } while (0)
31 
32 #define ANGLE_CL_IMPL_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, (void))
33 #define ANGLE_CL_IMPL_TRY_ERROR(EXPR, ERROR) \
34     ANGLE_TRY_TEMPLATE(EXPR, ANGLE_CL_RETURN_ERROR(ERROR); (void))
35 
36 namespace cl
37 {
38 
39 size_t GetChannelCount(cl_channel_order channelOrder);
40 
41 size_t GetElementSize(const cl_image_format &image_format);
42 
OverlapRegions(size_t offset1,size_t offset2,size_t size)43 inline bool OverlapRegions(size_t offset1, size_t offset2, size_t size)
44 {
45     // From https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html
46     // The regions overlap if src_offset <= dst_offset <= src_offset + size - 1
47     // or if dst_offset <= src_offset <= dst_offset + size - 1.
48     return (offset1 <= offset2 && offset2 <= offset1 + size - 1u) ||
49            (offset2 <= offset1 && offset1 <= offset2 + size - 1u);
50 }
51 
52 bool IsValidImageFormat(const cl_image_format *imageFormat, const rx::CLExtensions &extensions);
53 
54 extern thread_local cl_int gClErrorTls;
55 
56 }  // namespace cl
57 
58 #endif  // LIBANGLE_CL_UTILS_H_
59