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 // angle_cl.h: Includes all necessary CL headers and definitions for ANGLE. 7 8 #ifndef ANGLECL_H_ 9 #define ANGLECL_H_ 10 11 #define CL_TARGET_OPENCL_VERSION 300 12 #define CL_USE_DEPRECATED_OPENCL_1_0_APIS 13 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS 14 #define CL_USE_DEPRECATED_OPENCL_1_2_APIS 15 #define CL_USE_DEPRECATED_OPENCL_2_0_APIS 16 #define CL_USE_DEPRECATED_OPENCL_2_1_APIS 17 #define CL_USE_DEPRECATED_OPENCL_2_2_APIS 18 19 #include "CL/cl_icd.h" 20 21 #include <cstddef> 22 #include <cstdint> 23 #include <type_traits> 24 25 namespace cl 26 { 27 28 using ContextErrorCB = void(CL_CALLBACK *)(const char *errinfo, 29 const void *private_info, 30 size_t cb, 31 void *user_data); 32 33 using MemoryCB = void(CL_CALLBACK *)(cl_mem memobj, void *user_data); 34 using ProgramCB = void(CL_CALLBACK *)(cl_program program, void *user_data); 35 using EventCB = void(CL_CALLBACK *)(cl_event event, cl_int event_command_status, void *user_data); 36 using UserFunc = void(CL_CALLBACK *)(void *args); 37 38 template <typename T = void> 39 struct Dispatch 40 { DispatchDispatch41 explicit Dispatch(std::uint32_t magic) : mDispatch(sDispatch), mMagic(magic) {} 42 getDispatchDispatch43 const cl_icd_dispatch &getDispatch() const { return *mDispatch; } 44 45 static const cl_icd_dispatch *sDispatch; 46 47 protected: 48 // This has to be the first member to be OpenCL ICD compatible 49 const cl_icd_dispatch *const mDispatch; 50 const std::uint32_t mMagic; 51 }; 52 53 template <typename T> 54 const cl_icd_dispatch *Dispatch<T>::sDispatch = nullptr; 55 56 template <typename NativeObjectType, std::uint32_t magic> 57 struct NativeObject : public Dispatch<> 58 { NativeObjectNativeObject59 NativeObject() : Dispatch<>(magic) 60 { 61 static_assert(std::is_standard_layout<NativeObjectType>::value && 62 offsetof(NativeObjectType, mDispatch) == 0u, 63 "Not ICD compatible"); 64 } 65 66 template <typename T> castNativeObject67 T &cast() 68 { 69 return static_cast<T &>(*this); 70 } 71 72 template <typename T> castNativeObject73 const T &cast() const 74 { 75 return static_cast<const T &>(*this); 76 } 77 getNativeNativeObject78 NativeObjectType *getNative() { return static_cast<NativeObjectType *>(this); } 79 getNativeNativeObject80 const NativeObjectType *getNative() const 81 { 82 return static_cast<const NativeObjectType *>(this); 83 } 84 CastNativeNativeObject85 static NativeObjectType *CastNative(NativeObjectType *p) { return p; } 86 IsValidNativeObject87 static bool IsValid(const NativeObjectType *p) 88 { 89 return p != nullptr && p->mDispatch == sDispatch && p->mMagic == magic; 90 } 91 }; 92 93 } // namespace cl 94 95 struct _cl_platform_id : public cl::NativeObject<_cl_platform_id, 0x12345678u> 96 {}; 97 98 struct _cl_device_id : public cl::NativeObject<_cl_device_id, 0x23456789u> 99 {}; 100 101 struct _cl_context : public cl::NativeObject<_cl_context, 0x3456789Au> 102 {}; 103 104 struct _cl_command_queue : public cl::NativeObject<_cl_command_queue, 0x456789ABu> 105 {}; 106 107 struct _cl_mem : public cl::NativeObject<_cl_mem, 0x56789ABCu> 108 {}; 109 110 struct _cl_program : public cl::NativeObject<_cl_program, 0x6789ABCDu> 111 {}; 112 113 struct _cl_kernel : public cl::NativeObject<_cl_kernel, 0x789ABCDEu> 114 {}; 115 116 struct _cl_event : public cl::NativeObject<_cl_event, 0x89ABCDEFu> 117 {}; 118 119 struct _cl_sampler : public cl::NativeObject<_cl_sampler, 0x9ABCDEF0u> 120 {}; 121 122 #endif // ANGLECL_H_ 123