xref: /aosp_15_r20/external/deqp/external/vulkancts/framework/vulkan/vkPlatform.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _VKPLATFORM_HPP
2 #define _VKPLATFORM_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2015 Google Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Vulkan platform abstraction.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "vkDefs.hpp"
27 #include "deUniquePtr.hpp"
28 
29 #include <ostream>
30 #include <deSharedPtr.hpp>
31 #ifdef CTS_USES_VULKANSC
32 #include <mutex>
33 #include <vector>
34 #include <map>
35 #include "vkResourceInterface.hpp"
36 #endif // CTS_USES_VULKANSC
37 #include "tcuCommandLine.hpp"
38 
39 namespace tcu
40 {
41 class FunctionLibrary;
42 }
43 
44 namespace vk
45 {
46 
47 class Library
48 {
49 public:
Library(void)50     Library(void)
51     {
52     }
~Library(void)53     virtual ~Library(void)
54     {
55     }
56 
57     virtual const PlatformInterface &getPlatformInterface(void) const  = 0;
58     virtual const tcu::FunctionLibrary &getFunctionLibrary(void) const = 0;
59 };
60 
61 class PlatformDriver : public PlatformInterface
62 {
63 public:
64     PlatformDriver(const tcu::FunctionLibrary &library);
65     ~PlatformDriver(void);
66 
67 #include "vkConcretePlatformInterface.inl"
68 
getGetInstanceProcAddr() const69     virtual GetInstanceProcAddrFunc getGetInstanceProcAddr() const
70     {
71         return m_vk.getInstanceProcAddr;
72     }
73 
74 protected:
75     struct Functions
76     {
77 #include "vkPlatformFunctionPointers.inl"
78     };
79 
80     Functions m_vk;
81 };
82 
83 class InstanceDriver : public InstanceInterface
84 {
85 public:
86     InstanceDriver(const PlatformInterface &platformInterface, VkInstance instance);
87     virtual ~InstanceDriver(void);
88 
89 #include "vkConcreteInstanceInterface.inl"
90 
91 protected:
92     void loadFunctions(const PlatformInterface &platformInterface, VkInstance instance);
93 
94     struct Functions
95     {
96 #include "vkInstanceFunctionPointers.inl"
97     };
98 
99     Functions m_vk;
100 };
101 
102 #ifdef CTS_USES_VULKANSC
103 
104 class InstanceDriverSC : public InstanceDriver
105 {
106 public:
107     InstanceDriverSC(const PlatformInterface &platformInterface, VkInstance instance, const tcu::CommandLine &cmdLine,
108                      de::SharedPtr<vk::ResourceInterface> resourceInterface);
109 
110     virtual VkResult createDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
111                                   const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) const;
112 
113 protected:
114     mutable std::mutex functionMutex;
115     bool m_normalMode;
116     de::SharedPtr<vk::ResourceInterface> m_resourceInterface;
117 };
118 
119 #endif // CTS_USES_VULKANSC
120 
121 class DeviceDriver : public DeviceInterface
122 {
123 public:
124     DeviceDriver(const PlatformInterface &platformInterface, VkInstance instance, VkDevice device,
125                  uint32_t usedApiVersion, const tcu::CommandLine &cmdLine);
126     virtual ~DeviceDriver(void);
127 
128 #include "vkConcreteDeviceInterface.inl"
129 
130 #ifdef CTS_USES_VULKANSC
131     virtual VkResult createShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
132                                         const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const;
133 #endif // CTS_USES_VULKANSC
134 
135 protected:
136     struct Functions
137     {
138 #include "vkDeviceFunctionPointers.inl"
139     };
140 
141     bool m_computeOnlyMode;
142     Functions m_vk;
143 };
144 
145 #ifdef CTS_USES_VULKANSC
146 
147 #define DDSTAT_LOCK() std::lock_guard<std::mutex> statLock(m_resourceInterface->getStatMutex())
148 #define DDSTAT_HANDLE_CREATE(VAR_NAME, VAR_VALUE)                                                                \
149     do                                                                                                           \
150     {                                                                                                            \
151         m_resourceInterface->getStatCurrent().VAR_NAME += (VAR_VALUE);                                           \
152         m_resourceInterface->getStatMax().VAR_NAME =                                                             \
153             de::max(m_resourceInterface->getStatMax().VAR_NAME, m_resourceInterface->getStatCurrent().VAR_NAME); \
154     } while (0)
155 #define DDSTAT_HANDLE_DESTROY_IF(VAR_VARIABLE, VAR_NAME, VAR_VALUE)                             \
156     if (VAR_VARIABLE.getInternal() != DE_NULL && m_resourceInterface->isEnabledHandleDestroy()) \
157     m_resourceInterface->getStatCurrent().VAR_NAME -= (VAR_VALUE)
158 #define DDSTAT_HANDLE_DESTROY(VAR_NAME, VAR_VALUE)     \
159     if (m_resourceInterface->isEnabledHandleDestroy()) \
160     m_resourceInterface->getStatCurrent().VAR_NAME -= (VAR_VALUE)
161 
162 class DeviceDriverSC : public DeviceDriver
163 {
164 public:
165     DeviceDriverSC(const PlatformInterface &platformInterface, VkInstance instance, VkDevice device,
166                    const tcu::CommandLine &cmdLine, de::SharedPtr<vk::ResourceInterface> resourceInterface,
167                    const VkPhysicalDeviceVulkanSC10Properties &physicalDeviceVulkanSC10Properties,
168                    const VkPhysicalDeviceProperties &physicalDeviceProperties, const uint32_t usedApiVersion);
169     virtual ~DeviceDriverSC(void);
170 
171 #include "vkConcreteDeviceInterface.inl"
172 
173     // Functions ending with Handler() and HandlerStat() work only when we gather statistics ( in a main process ).
174     // Functions ending with HandlerNorm() work in normal mode ( in subprocess, when real test is performed )
175     // Method createShaderModule() works in both modes, and ResourceInterface is responsible for distinguishing modes
176     void destroyDeviceHandler(VkDevice device, const VkAllocationCallbacks *pAllocator) const;
177     VkResult createDescriptorSetLayoutHandlerNorm(VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
178                                                   const VkAllocationCallbacks *pAllocator,
179                                                   VkDescriptorSetLayout *pSetLayout) const;
180     void createDescriptorSetLayoutHandlerStat(VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
181                                               const VkAllocationCallbacks *pAllocator,
182                                               VkDescriptorSetLayout *pSetLayout) const;
183     void destroyDescriptorSetLayoutHandler(VkDevice device, VkDescriptorSetLayout descriptorSetLayout,
184                                            const VkAllocationCallbacks *pAllocator) const;
185     void allocateDescriptorSetsHandlerStat(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
186                                            VkDescriptorSet *pDescriptorSets) const;
187     void freeDescriptorSetsHandlerStat(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
188                                        const VkDescriptorSet *pDescriptorSets) const;
189     void resetDescriptorPoolHandlerStat(VkDevice device, VkDescriptorPool descriptorPool,
190                                         VkDescriptorPoolResetFlags flags) const;
191     void createImageViewHandler(VkDevice device, const VkImageViewCreateInfo *pCreateInfo,
192                                 const VkAllocationCallbacks *pAllocator, VkImageView *pView) const;
193     void destroyImageViewHandler(VkDevice device, VkImageView imageView, const VkAllocationCallbacks *pAllocator) const;
194     void createQueryPoolHandler(VkDevice device, const VkQueryPoolCreateInfo *pCreateInfo,
195                                 const VkAllocationCallbacks *pAllocator, VkQueryPool *pQueryPool) const;
196     VkResult createPipelineLayoutHandlerNorm(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
197                                              const VkAllocationCallbacks *pAllocator,
198                                              VkPipelineLayout *pPipelineLayout) const;
199     void createPipelineLayoutHandlerStat(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
200                                          const VkAllocationCallbacks *pAllocator,
201                                          VkPipelineLayout *pPipelineLayout) const;
202     VkResult createGraphicsPipelinesHandlerNorm(VkDevice device, VkPipelineCache pipelineCache,
203                                                 uint32_t createInfoCount,
204                                                 const VkGraphicsPipelineCreateInfo *pCreateInfos,
205                                                 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) const;
206     void createGraphicsPipelinesHandlerStat(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
207                                             const VkGraphicsPipelineCreateInfo *pCreateInfos,
208                                             const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) const;
209     VkResult createComputePipelinesHandlerNorm(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
210                                                const VkComputePipelineCreateInfo *pCreateInfos,
211                                                const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) const;
212     void createComputePipelinesHandlerStat(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount,
213                                            const VkComputePipelineCreateInfo *pCreateInfos,
214                                            const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines) const;
215     void destroyPipelineHandler(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks *pAllocator) const;
216     VkResult createFramebufferHandlerNorm(VkDevice device, const VkFramebufferCreateInfo *pCreateInfo,
217                                           const VkAllocationCallbacks *pAllocator, VkFramebuffer *pFramebuffer) const;
218     void createFramebufferHandlerStat(VkDevice device, const VkFramebufferCreateInfo *pCreateInfo,
219                                       const VkAllocationCallbacks *pAllocator, VkFramebuffer *pFramebuffer) const;
220     VkResult createRenderPassHandlerNorm(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
221                                          const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass) const;
222     void createRenderPassHandlerStat(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
223                                      const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass) const;
224     VkResult createRenderPass2HandlerNorm(VkDevice device, const VkRenderPassCreateInfo2 *pCreateInfo,
225                                           const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass) const;
226     void createRenderPass2HandlerStat(VkDevice device, const VkRenderPassCreateInfo2 *pCreateInfo,
227                                       const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass) const;
228     void destroyRenderPassHandler(VkDevice device, VkRenderPass renderPass,
229                                   const VkAllocationCallbacks *pAllocator) const;
230     VkResult createSamplerHandlerNorm(VkDevice device, const VkSamplerCreateInfo *pCreateInfo,
231                                       const VkAllocationCallbacks *pAllocator, VkSampler *pSampler) const;
232     void createSamplerHandlerStat(VkDevice device, const VkSamplerCreateInfo *pCreateInfo,
233                                   const VkAllocationCallbacks *pAllocator, VkSampler *pSampler) const;
234     VkResult createSamplerYcbcrConversionHandlerNorm(VkDevice device,
235                                                      const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
236                                                      const VkAllocationCallbacks *pAllocator,
237                                                      VkSamplerYcbcrConversion *pYcbcrConversion) const;
238     void createSamplerYcbcrConversionHandlerStat(VkDevice device, const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
239                                                  const VkAllocationCallbacks *pAllocator,
240                                                  VkSamplerYcbcrConversion *pYcbcrConversion) const;
241     void getDescriptorSetLayoutSupportHandler(VkDevice device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
242                                               VkDescriptorSetLayoutSupport *pSupport) const;
243     virtual VkResult createShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
244                                         const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const;
245 
246     VkResult createCommandPoolHandlerNorm(VkDevice device, const VkCommandPoolCreateInfo *pCreateInfo,
247                                           const VkAllocationCallbacks *pAllocator, VkCommandPool *pCommandPool) const;
248     VkResult resetCommandPoolHandlerNorm(VkDevice device, VkCommandPool commandPool,
249                                          VkCommandPoolResetFlags flags) const;
250     void createCommandPoolHandlerStat(VkDevice device, const VkCommandPoolCreateInfo *pCreateInfo,
251                                       const VkAllocationCallbacks *pAllocator, VkCommandPool *pCommandPool) const;
252     void resetCommandPoolHandlerStat(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) const;
253     void allocateCommandBuffersHandler(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
254                                        VkCommandBuffer *pCommandBuffers) const;
255     void freeCommandBuffersHandler(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
256                                    const VkCommandBuffer *pCommandBuffers) const;
257     void increaseCommandBufferSize(VkCommandBuffer commandBuffer, VkDeviceSize commandSize) const;
258     void checkFramebufferSupport(const VkFramebufferCreateInfo *pCreateInfo) const;
259     void checkRenderPassSupport(uint32_t attachmentCount, uint32_t subpassCount, uint32_t dependencyCount) const;
260     void checkSubpassSupport(uint32_t inputAttachmentCount, uint32_t preserveAttachmentCount) const;
261 
262     de::SharedPtr<ResourceInterface> gerResourceInterface() const;
263     void reset() const;
264 
265 protected:
266     mutable std::mutex functionMutex;
267     bool m_normalMode;
268 
269     de::SharedPtr<vk::ResourceInterface> m_resourceInterface;
270 
271     mutable std::vector<uint8_t> m_falseMemory;
272     mutable std::map<VkImageView, VkImageViewCreateInfo> m_imageViews;
273     mutable std::map<VkDescriptorSetLayout, VkDescriptorSetLayoutCreateInfo> m_descriptorSetLayouts;
274     mutable std::map<VkRenderPass, VkRenderPassCreateInfo> m_renderPasses;
275     mutable std::map<VkRenderPass, VkRenderPassCreateInfo2> m_renderPasses2;
276     mutable std::map<VkPipeline, VkGraphicsPipelineCreateInfo> m_graphicsPipelines;
277     mutable std::map<VkPipeline, VkComputePipelineCreateInfo> m_computePipelines;
278     mutable std::map<VkDescriptorSet, VkDescriptorPool> m_descriptorSetsInPool;
279     VkPhysicalDeviceVulkanSC10Properties m_physicalDeviceVulkanSC10Properties;
280     VkPhysicalDeviceProperties m_physicalDeviceProperties;
281 
282     VkDeviceSize m_commandDefaultSize;
283     VkDeviceSize m_commandBufferMinimumSize;
284     VkDeviceSize m_commandPoolMinimumSize;
285 };
286 
287 class DeinitDeviceDeleter : public Deleter<DeviceDriverSC>
288 {
289 public:
DeinitDeviceDeleter(ResourceInterface * resourceInterface,const VkDevice & device)290     DeinitDeviceDeleter(ResourceInterface *resourceInterface, const VkDevice &device)
291         : m_resourceInterface(resourceInterface)
292         , m_device(device)
293     {
294     }
DeinitDeviceDeleter(void)295     DeinitDeviceDeleter(void) : m_resourceInterface(DE_NULL), m_device(DE_NULL)
296     {
297     }
298 
operator ()(DeviceDriverSC * obj) const299     void operator()(DeviceDriverSC *obj) const
300     {
301         if (m_resourceInterface != DE_NULL)
302             m_resourceInterface->deinitDevice(m_device);
303         delete obj;
304     }
305 
306 private:
307     ResourceInterface *m_resourceInterface;
308     VkDevice m_device;
309 };
310 
311 #endif // CTS_USES_VULKANSC
312 #define THROW_NOT_SUPPORTED_COMPUTE_ONLY() TCU_THROW(NotSupportedError, "Not compute-only")
313 
314 // Single device driver pointer type which will differ in SC and non-SC mode helping clearing the code
315 #ifndef CTS_USES_VULKANSC
316 typedef de::MovePtr<DeviceDriver> DeviceDriverPtr;
317 #else
318 typedef de::MovePtr<DeviceDriverSC, vk::DeinitDeviceDeleter> DeviceDriverPtr;
319 #endif // CTS_USES_VULKANSC
320 
321 // Defined in vkWsiPlatform.hpp
322 namespace wsi
323 {
324 class Display;
325 } // namespace wsi
326 
327 /*--------------------------------------------------------------------*//*!
328  * \brief Vulkan platform interface
329  *//*--------------------------------------------------------------------*/
330 class Platform
331 {
332 public:
333     enum LibraryType
334     {
335         LIBRARY_TYPE_VULKAN = 0,
336         LIBRARY_TYPE_LAST
337     };
338 
Platform(void)339     Platform(void)
340     {
341     }
~Platform(void)342     virtual ~Platform(void)
343     {
344     }
345 #ifdef DE_PLATFORM_USE_LIBRARY_TYPE
346     virtual Library *createLibrary(LibraryType libraryType = LIBRARY_TYPE_VULKAN,
347                                    const char *libraryPath = DE_NULL) const = 0;
348 #else
349     virtual Library *createLibrary(const char *libraryPath = DE_NULL) const = 0;
350 #endif
351 
352     virtual wsi::Display *createWsiDisplay(wsi::Type wsiType) const;
353     virtual bool hasDisplay(wsi::Type wsiType) const;
354     virtual void describePlatform(std::ostream &dst) const;
355 };
356 
357 } // namespace vk
358 
359 #endif // _VKPLATFORM_HPP
360