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