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_utils.h"
14
15 #include "libANGLE/renderer/CLContextImpl.h"
16
17 #include <libANGLE/CLContext.h>
18 #include "libANGLE/CLDevice.h"
19
20 namespace rx
21 {
22
23 class CLContextVk : public CLContextImpl, public vk::Context
24 {
25 public:
26 CLContextVk(const cl::Context &context, const cl::DevicePtrs devicePtrs);
27
28 ~CLContextVk() override;
29
30 void handleError(VkResult errorCode,
31 const char *file,
32 const char *function,
33 unsigned int line) override;
34
35 bool hasMemory(cl_mem memory) const;
36
37 angle::Result getDevices(cl::DevicePtrs *devicePtrsOut) const override;
38
39 angle::Result createCommandQueue(const cl::CommandQueue &commandQueue,
40 CLCommandQueueImpl::Ptr *commandQueueOut) override;
41
42 angle::Result createBuffer(const cl::Buffer &buffer,
43 void *hostPtr,
44 CLMemoryImpl::Ptr *bufferOut) override;
45
46 angle::Result createImage(const cl::Image &image,
47 cl::MemFlags flags,
48 const cl_image_format &format,
49 const cl::ImageDescriptor &desc,
50 void *hostPtr,
51 CLMemoryImpl::Ptr *imageOut) override;
52
53 angle::Result getSupportedImageFormats(cl::MemFlags flags,
54 cl::MemObjectType imageType,
55 cl_uint numEntries,
56 cl_image_format *imageFormats,
57 cl_uint *numImageFormats) override;
58
59 angle::Result createSampler(const cl::Sampler &sampler,
60 CLSamplerImpl::Ptr *samplerOut) override;
61
62 angle::Result createProgramWithSource(const cl::Program &program,
63 const std::string &source,
64 CLProgramImpl::Ptr *programOut) override;
65
66 angle::Result createProgramWithIL(const cl::Program &program,
67 const void *il,
68 size_t length,
69 CLProgramImpl::Ptr *programOut) override;
70
71 angle::Result createProgramWithBinary(const cl::Program &program,
72 const size_t *lengths,
73 const unsigned char **binaries,
74 cl_int *binaryStatus,
75 CLProgramImpl::Ptr *programOut) override;
76
77 angle::Result createProgramWithBuiltInKernels(const cl::Program &program,
78 const char *kernel_names,
79 CLProgramImpl::Ptr *programOut) override;
80
81 angle::Result linkProgram(const cl::Program &program,
82 const cl::DevicePtrs &devices,
83 const char *options,
84 const cl::ProgramPtrs &inputPrograms,
85 cl::Program *notify,
86 CLProgramImpl::Ptr *programOut) override;
87
88 angle::Result createUserEvent(const cl::Event &event, CLEventImpl::Ptr *eventOut) override;
89
90 angle::Result waitForEvents(const cl::EventPtrs &events) override;
91
getPlatform()92 CLPlatformVk *getPlatform() { return &mContext.getPlatform().getImpl<CLPlatformVk>(); }
93
getFrontendObject()94 cl::Context &getFrontendObject() { return const_cast<cl::Context &>(mContext); }
95
96 private:
97 void handleDeviceLost() const;
98
99 // Have the CL Context keep tabs on associated CL objects
100 struct Mutable
101 {
102 std::unordered_set<const _cl_mem *> mMemories;
103 };
104 using MutableData = angle::SynchronizedValue<Mutable>;
105 MutableData mAssociatedObjects;
106
107 const cl::DevicePtrs mAssociatedDevices;
108
109 friend class CLMemoryVk;
110 };
111
hasMemory(cl_mem memory)112 inline bool CLContextVk::hasMemory(cl_mem memory) const
113 {
114 const auto data = mAssociatedObjects.synchronize();
115 return data->mMemories.find(memory) != data->mMemories.cend();
116 }
117
118 } // namespace rx
119
120 #endif // LIBANGLE_RENDERER_VULKAN_CLCONTEXTVK_H_
121