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