xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/vulkan/CLContextVk.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 // CLContextVk.h: Defines the class interface for CLContextVk, implementing CLContextImpl.
7 
8 #ifndef LIBANGLE_RENDERER_VULKAN_CLCONTEXTVK_H_
9 #define LIBANGLE_RENDERER_VULKAN_CLCONTEXTVK_H_
10 
11 #include "libANGLE/renderer/vulkan/CLPlatformVk.h"
12 #include "libANGLE/renderer/vulkan/cl_types.h"
13 #include "libANGLE/renderer/vulkan/vk_cache_utils.h"
14 #include "libANGLE/renderer/vulkan/vk_utils.h"
15 
16 #include "libANGLE/renderer/CLContextImpl.h"
17 
18 #include <libANGLE/CLContext.h>
19 #include "libANGLE/CLDevice.h"
20 
21 namespace rx
22 {
23 
24 class CLContextVk : public CLContextImpl, public vk::Context
25 {
26   public:
27     CLContextVk(const cl::Context &context, const cl::DevicePtrs devicePtrs);
28 
29     ~CLContextVk() override;
30 
31     void handleError(VkResult errorCode,
32                      const char *file,
33                      const char *function,
34                      unsigned int line) override;
35 
36     bool hasMemory(cl_mem memory) const;
37 
38     angle::Result getDevices(cl::DevicePtrs *devicePtrsOut) const override;
39 
40     angle::Result createCommandQueue(const cl::CommandQueue &commandQueue,
41                                      CLCommandQueueImpl::Ptr *commandQueueOut) override;
42 
43     angle::Result createBuffer(const cl::Buffer &buffer,
44                                void *hostPtr,
45                                CLMemoryImpl::Ptr *bufferOut) override;
46 
47     angle::Result createImage(const cl::Image &image,
48                               void *hostPtr,
49                               CLMemoryImpl::Ptr *imageOut) override;
50 
51     angle::Result getSupportedImageFormats(cl::MemFlags flags,
52                                            cl::MemObjectType imageType,
53                                            cl_uint numEntries,
54                                            cl_image_format *imageFormats,
55                                            cl_uint *numImageFormats) override;
56 
57     angle::Result createSampler(const cl::Sampler &sampler,
58                                 CLSamplerImpl::Ptr *samplerOut) override;
59 
60     angle::Result createProgramWithSource(const cl::Program &program,
61                                           const std::string &source,
62                                           CLProgramImpl::Ptr *programOut) override;
63 
64     angle::Result createProgramWithIL(const cl::Program &program,
65                                       const void *il,
66                                       size_t length,
67                                       CLProgramImpl::Ptr *programOut) override;
68 
69     angle::Result createProgramWithBinary(const cl::Program &program,
70                                           const size_t *lengths,
71                                           const unsigned char **binaries,
72                                           cl_int *binaryStatus,
73                                           CLProgramImpl::Ptr *programOut) override;
74 
75     angle::Result createProgramWithBuiltInKernels(const cl::Program &program,
76                                                   const char *kernel_names,
77                                                   CLProgramImpl::Ptr *programOut) override;
78 
79     angle::Result linkProgram(const cl::Program &program,
80                               const cl::DevicePtrs &devices,
81                               const char *options,
82                               const cl::ProgramPtrs &inputPrograms,
83                               cl::Program *notify,
84                               CLProgramImpl::Ptr *programOut) override;
85 
86     angle::Result createUserEvent(const cl::Event &event, CLEventImpl::Ptr *eventOut) override;
87 
88     angle::Result waitForEvents(const cl::EventPtrs &events) override;
89 
getPlatform()90     CLPlatformVk *getPlatform() { return &mContext.getPlatform().getImpl<CLPlatformVk>(); }
91 
getFrontendObject()92     cl::Context &getFrontendObject() { return const_cast<cl::Context &>(mContext); }
93 
getDescriptorSetLayoutCache()94     DescriptorSetLayoutCache *getDescriptorSetLayoutCache() { return &mDescriptorSetLayoutCache; }
getPipelineLayoutCache()95     PipelineLayoutCache *getPipelineLayoutCache() { return &mPipelineLayoutCache; }
96 
97   private:
98     void handleDeviceLost() const;
99     VkFormat getVkFormatFromCL(cl_image_format format);
100 
101     // Caches for DescriptorSetLayout and PipelineLayout
102     DescriptorSetLayoutCache mDescriptorSetLayoutCache;
103     PipelineLayoutCache mPipelineLayoutCache;
104 
105     // Have the CL Context keep tabs on associated CL objects
106     struct Mutable
107     {
108         std::unordered_set<const _cl_mem *> mMemories;
109     };
110     using MutableData = angle::SynchronizedValue<Mutable>;
111     MutableData mAssociatedObjects;
112 
113     const cl::DevicePtrs mAssociatedDevices;
114 
115     // Table of minimum required image formats for OpenCL
116     static constexpr cl_image_format kMinSupportedFormatsReadOrWrite[11] = {
117         {CL_RGBA, CL_UNORM_INT8},  {CL_RGBA, CL_UNSIGNED_INT8},  {CL_RGBA, CL_SIGNED_INT8},
118         {CL_RGBA, CL_UNORM_INT16}, {CL_RGBA, CL_UNSIGNED_INT16}, {CL_RGBA, CL_SIGNED_INT16},
119         {CL_RGBA, CL_HALF_FLOAT},  {CL_RGBA, CL_UNSIGNED_INT32}, {CL_RGBA, CL_SIGNED_INT32},
120         {CL_RGBA, CL_FLOAT},       {CL_BGRA, CL_UNORM_INT8}};
121 
122     static constexpr cl_image_format kMinSupportedFormatsReadAndWrite[18] = {
123         {CL_R, CL_UNORM_INT8},        {CL_R, CL_UNSIGNED_INT8},    {CL_R, CL_SIGNED_INT8},
124         {CL_R, CL_UNSIGNED_INT16},    {CL_R, CL_SIGNED_INT16},     {CL_R, CL_HALF_FLOAT},
125         {CL_R, CL_UNSIGNED_INT32},    {CL_R, CL_SIGNED_INT32},     {CL_R, CL_FLOAT},
126         {CL_RGBA, CL_UNORM_INT8},     {CL_RGBA, CL_UNSIGNED_INT8}, {CL_RGBA, CL_SIGNED_INT8},
127         {CL_RGBA, CL_UNSIGNED_INT16}, {CL_RGBA, CL_SIGNED_INT16},  {CL_RGBA, CL_HALF_FLOAT},
128         {CL_RGBA, CL_UNSIGNED_INT32}, {CL_RGBA, CL_SIGNED_INT32},  {CL_RGBA, CL_FLOAT}};
129 
130     friend class CLMemoryVk;
131 };
132 
hasMemory(cl_mem memory)133 inline bool CLContextVk::hasMemory(cl_mem memory) const
134 {
135     const auto data = mAssociatedObjects.synchronize();
136     return data->mMemories.find(memory) != data->mMemories.cend();
137 }
138 
139 }  // namespace rx
140 
141 #endif  // LIBANGLE_RENDERER_VULKAN_CLCONTEXTVK_H_
142