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 #include "tcuCommandLine.hpp"
37 #endif // CTS_USES_VULKANSC
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) {}
~Library(void)51 	virtual								~Library				(void) {}
52 
53 	virtual const PlatformInterface&	getPlatformInterface	(void) const = 0;
54 	virtual const tcu::FunctionLibrary&	getFunctionLibrary		(void) const = 0;
55 };
56 
57 class PlatformDriver : public PlatformInterface
58 {
59 public:
60 				PlatformDriver	(const tcu::FunctionLibrary& library);
61 				~PlatformDriver	(void);
62 
63 #include "vkConcretePlatformInterface.inl"
64 
getGetInstanceProcAddr() const65 				virtual	GetInstanceProcAddrFunc	getGetInstanceProcAddr  () const {
66 					return m_vk.getInstanceProcAddr;
67 				}
68 
69 protected:
70 	struct Functions
71 	{
72 #include "vkPlatformFunctionPointers.inl"
73 	};
74 
75 	Functions	m_vk;
76 };
77 
78 class InstanceDriver : public InstanceInterface
79 {
80 public:
81 						InstanceDriver		(const PlatformInterface&	platformInterface,
82 											 VkInstance					instance);
83 	virtual				~InstanceDriver		(void);
84 
85 #include "vkConcreteInstanceInterface.inl"
86 
87 protected:
88 	void				loadFunctions		(const PlatformInterface&	platformInterface,
89 											 VkInstance					instance);
90 
91 	struct Functions
92 	{
93 #include "vkInstanceFunctionPointers.inl"
94 	};
95 
96 	Functions	m_vk;
97 };
98 
99 #ifdef CTS_USES_VULKANSC
100 
101 class InstanceDriverSC : public InstanceDriver
102 {
103 public:
104 						InstanceDriverSC	(const PlatformInterface&				platformInterface,
105 											 VkInstance								instance,
106 											 const tcu::CommandLine&				cmdLine,
107 											 de::SharedPtr<vk::ResourceInterface>	resourceInterface);
108 
109 	virtual VkResult	createDevice		(VkPhysicalDevice						physicalDevice,
110 											 const VkDeviceCreateInfo*				pCreateInfo,
111 											 const VkAllocationCallbacks*			pAllocator,
112 											 VkDevice*								pDevice) const;
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,
125 											 VkInstance							instance,
126 											 VkDevice							device,
127 											 uint32_t							usedApiVersion);
128 	virtual				~DeviceDriver		(void);
129 
130 #include "vkConcreteDeviceInterface.inl"
131 
132 #ifdef CTS_USES_VULKANSC
133 	virtual VkResult	createShaderModule	(VkDevice							device,
134 											 const VkShaderModuleCreateInfo*	pCreateInfo,
135 											 const VkAllocationCallbacks*		pAllocator,
136 											 VkShaderModule*					pShaderModule) const;
137 #endif // CTS_USES_VULKANSC
138 
139 protected:
140 	struct Functions
141 	{
142 #include "vkDeviceFunctionPointers.inl"
143 	};
144 
145 	Functions	m_vk;
146 };
147 
148 #ifdef CTS_USES_VULKANSC
149 
150 #define DDSTAT_LOCK() std::lock_guard<std::mutex> statLock(m_resourceInterface->getStatMutex())
151 #define DDSTAT_HANDLE_CREATE(VAR_NAME,VAR_VALUE) do { m_resourceInterface->getStatCurrent().VAR_NAME += (VAR_VALUE); m_resourceInterface->getStatMax().VAR_NAME = de::max(m_resourceInterface->getStatMax().VAR_NAME, m_resourceInterface->getStatCurrent().VAR_NAME); } while(0)
152 #define DDSTAT_HANDLE_DESTROY_IF(VAR_VARIABLE,VAR_NAME,VAR_VALUE) if(VAR_VARIABLE.getInternal()!=DE_NULL && m_resourceInterface->isEnabledHandleDestroy()) m_resourceInterface->getStatCurrent().VAR_NAME -= (VAR_VALUE)
153 #define DDSTAT_HANDLE_DESTROY(VAR_NAME,VAR_VALUE) if( m_resourceInterface->isEnabledHandleDestroy() ) m_resourceInterface->getStatCurrent().VAR_NAME -= (VAR_VALUE)
154 
155 class DeviceDriverSC : public DeviceDriver
156 {
157 public:
158 										DeviceDriverSC						(const		PlatformInterface&					platformInterface,
159 																			 VkInstance										instance,
160 																			 VkDevice										device,
161 																			 const tcu::CommandLine&						cmdLine,
162 																			 de::SharedPtr<vk::ResourceInterface>			resourceInterface,
163 																			 const VkPhysicalDeviceVulkanSC10Properties&	physicalDeviceVulkanSC10Properties,
164 																			 const VkPhysicalDeviceProperties&				physicalDeviceProperties,
165 																			 const uint32_t									usedApiVersion);
166 	virtual								~DeviceDriverSC						(void);
167 
168 #include "vkConcreteDeviceInterface.inl"
169 
170 	// Functions ending with Handler() and HandlerStat() work only when we gather statistics ( in a main process ).
171 	// Functions ending with HandlerNorm() work in normal mode ( in subprocess, when real test is performed )
172 	// Method createShaderModule() works in both modes, and ResourceInterface is responsible for distinguishing modes
173 	void								destroyDeviceHandler					(VkDevice								device,
174 																				 const VkAllocationCallbacks*			pAllocator) const;
175 	VkResult							createDescriptorSetLayoutHandlerNorm	(VkDevice								device,
176 																				 const VkDescriptorSetLayoutCreateInfo*	pCreateInfo,
177 																				 const VkAllocationCallbacks*			pAllocator,
178 																				 VkDescriptorSetLayout*					pSetLayout) const;
179 	void								createDescriptorSetLayoutHandlerStat	(VkDevice								device,
180 																				 const VkDescriptorSetLayoutCreateInfo*	pCreateInfo,
181 																				 const VkAllocationCallbacks*			pAllocator,
182 																				 VkDescriptorSetLayout*					pSetLayout) const;
183 	void								destroyDescriptorSetLayoutHandler		(VkDevice								device,
184 																				 VkDescriptorSetLayout					descriptorSetLayout,
185 																				 const VkAllocationCallbacks*			pAllocator) const;
186 	void								allocateDescriptorSetsHandlerStat		(VkDevice								device,
187 																				 const VkDescriptorSetAllocateInfo*		pAllocateInfo,
188 																				 VkDescriptorSet*						pDescriptorSets) const;
189 	void								freeDescriptorSetsHandlerStat			(VkDevice								device,
190 																				 VkDescriptorPool						descriptorPool,
191 																				 uint32_t								descriptorSetCount,
192 																				 const VkDescriptorSet*					pDescriptorSets) const;
193 	void								resetDescriptorPoolHandlerStat			(VkDevice								device,
194 																				 VkDescriptorPool						descriptorPool,
195 																				 VkDescriptorPoolResetFlags				flags) const;
196 	void								createImageViewHandler					(VkDevice								device,
197 																				 const VkImageViewCreateInfo*			pCreateInfo,
198 																				 const VkAllocationCallbacks*			pAllocator,
199 																				 VkImageView*							pView) const;
200 	void								destroyImageViewHandler					(VkDevice								device,
201 																				 VkImageView							imageView,
202 																				 const VkAllocationCallbacks*			pAllocator) const;
203 	void								createQueryPoolHandler					(VkDevice								device,
204 																				 const VkQueryPoolCreateInfo*			pCreateInfo,
205 																				 const VkAllocationCallbacks*			pAllocator,
206 																				 VkQueryPool*							pQueryPool) const ;
207 	VkResult							createPipelineLayoutHandlerNorm			(VkDevice								device,
208 																				 const VkPipelineLayoutCreateInfo*		pCreateInfo,
209 																				 const VkAllocationCallbacks*			pAllocator,
210 																				 VkPipelineLayout*						pPipelineLayout) const;
211 	void								createPipelineLayoutHandlerStat			(VkDevice								device,
212 																				 const VkPipelineLayoutCreateInfo*		pCreateInfo,
213 																				 const VkAllocationCallbacks*			pAllocator,
214 																				 VkPipelineLayout*						pPipelineLayout) const;
215 	VkResult							createGraphicsPipelinesHandlerNorm		(VkDevice								device,
216 																				 VkPipelineCache						pipelineCache,
217 																				 deUint32								createInfoCount,
218 																				 const VkGraphicsPipelineCreateInfo*	pCreateInfos,
219 																				 const VkAllocationCallbacks*			pAllocator,
220 																				 VkPipeline*							pPipelines) const;
221 	void								createGraphicsPipelinesHandlerStat		(VkDevice								device,
222 																				 VkPipelineCache						pipelineCache,
223 																				 deUint32								createInfoCount,
224 																				 const VkGraphicsPipelineCreateInfo*	pCreateInfos,
225 																				 const VkAllocationCallbacks*			pAllocator,
226 																				 VkPipeline*							pPipelines) const;
227 	VkResult							createComputePipelinesHandlerNorm		(VkDevice								device,
228 																				 VkPipelineCache						pipelineCache,
229 																				 deUint32								createInfoCount,
230 																				 const VkComputePipelineCreateInfo*		pCreateInfos,
231 																				 const VkAllocationCallbacks*			pAllocator,
232 																				 VkPipeline*							pPipelines) const;
233 	void								createComputePipelinesHandlerStat		(VkDevice								device,
234 																				 VkPipelineCache						pipelineCache,
235 																				 deUint32								createInfoCount,
236 																				 const VkComputePipelineCreateInfo*		pCreateInfos,
237 																				 const VkAllocationCallbacks*			pAllocator,
238 																				 VkPipeline*							pPipelines) const;
239 	void								destroyPipelineHandler					(VkDevice								device,
240 																				 VkPipeline								pipeline,
241 																				 const VkAllocationCallbacks*			pAllocator) const;
242 	VkResult							createFramebufferHandlerNorm			(VkDevice								device,
243 																				 const VkFramebufferCreateInfo*			pCreateInfo,
244 																				 const VkAllocationCallbacks*			pAllocator,
245 																				 VkFramebuffer*							pFramebuffer) const;
246 	void								createFramebufferHandlerStat			(VkDevice								device,
247 																				 const VkFramebufferCreateInfo*			pCreateInfo,
248 																				 const VkAllocationCallbacks*			pAllocator,
249 																				 VkFramebuffer*							pFramebuffer) const;
250 	VkResult							createRenderPassHandlerNorm				(VkDevice								device,
251 																				 const VkRenderPassCreateInfo*			pCreateInfo,
252 																				 const VkAllocationCallbacks*			pAllocator,
253 																				 VkRenderPass*							pRenderPass) const;
254 	void								createRenderPassHandlerStat				(VkDevice								device,
255 																				 const VkRenderPassCreateInfo*			pCreateInfo,
256 																				 const VkAllocationCallbacks*			pAllocator,
257 																				 VkRenderPass*							pRenderPass) const;
258 	VkResult							createRenderPass2HandlerNorm			(VkDevice								device,
259 																				 const VkRenderPassCreateInfo2*			pCreateInfo,
260 																				 const VkAllocationCallbacks*			pAllocator,
261 																				 VkRenderPass*							pRenderPass) const;
262 	void								createRenderPass2HandlerStat			(VkDevice								device,
263 																				 const VkRenderPassCreateInfo2*			pCreateInfo,
264 																				 const VkAllocationCallbacks*			pAllocator,
265 																				 VkRenderPass*							pRenderPass) const;
266 	void								destroyRenderPassHandler				(VkDevice								device,
267 																				 VkRenderPass							renderPass,
268 																				 const VkAllocationCallbacks*			pAllocator) const;
269 	VkResult							createSamplerHandlerNorm				(VkDevice								device,
270 																				 const VkSamplerCreateInfo*				pCreateInfo,
271 																				 const VkAllocationCallbacks*			pAllocator,
272 																				 VkSampler*								pSampler) const;
273 	void								createSamplerHandlerStat				(VkDevice								device,
274 																				 const VkSamplerCreateInfo*				pCreateInfo,
275 																				 const VkAllocationCallbacks*			pAllocator,
276 																				 VkSampler*								pSampler) const;
277 	VkResult							createSamplerYcbcrConversionHandlerNorm	(VkDevice								device,
278 																				 const VkSamplerYcbcrConversionCreateInfo*	pCreateInfo,
279 																				 const VkAllocationCallbacks*			pAllocator,
280 																				 VkSamplerYcbcrConversion*				pYcbcrConversion) const;
281 	void								createSamplerYcbcrConversionHandlerStat	(VkDevice								device,
282 																				 const VkSamplerYcbcrConversionCreateInfo*	pCreateInfo,
283 																				 const VkAllocationCallbacks*			pAllocator,
284 																				 VkSamplerYcbcrConversion*				pYcbcrConversion) const;
285 	void								getDescriptorSetLayoutSupportHandler	(VkDevice								device,
286 																				 const VkDescriptorSetLayoutCreateInfo*	pCreateInfo,
287 																				 VkDescriptorSetLayoutSupport*			pSupport) const;
288 	virtual VkResult					createShaderModule						(VkDevice								device,
289 																				 const	VkShaderModuleCreateInfo*		pCreateInfo,
290 																				 const VkAllocationCallbacks*			pAllocator,
291 																				 VkShaderModule*						pShaderModule) const;
292 
293 	VkResult							createCommandPoolHandlerNorm			(VkDevice								device,
294 																				 const VkCommandPoolCreateInfo*			pCreateInfo,
295 																				 const VkAllocationCallbacks*			pAllocator,
296 																				 VkCommandPool*							pCommandPool) const;
297 	VkResult							resetCommandPoolHandlerNorm				(VkDevice								device,
298 																				 VkCommandPool							commandPool,
299 																				 VkCommandPoolResetFlags				flags) const;
300 	void								createCommandPoolHandlerStat			(VkDevice								device,
301 																				 const VkCommandPoolCreateInfo*			pCreateInfo,
302 																				 const VkAllocationCallbacks*			pAllocator,
303 																				 VkCommandPool*							pCommandPool) const;
304 	void								resetCommandPoolHandlerStat				(VkDevice								device,
305 																				 VkCommandPool							commandPool,
306 																				 VkCommandPoolResetFlags				flags) const;
307 	void								allocateCommandBuffersHandler			(VkDevice								device,
308 																				 const VkCommandBufferAllocateInfo*		pAllocateInfo,
309 																				 VkCommandBuffer*						pCommandBuffers) const;
310 	void								freeCommandBuffersHandler				(VkDevice								device,
311 																				 VkCommandPool							commandPool,
312 																				 deUint32								commandBufferCount,
313 																				 const VkCommandBuffer*					pCommandBuffers) const;
314 	void								increaseCommandBufferSize				(VkCommandBuffer						commandBuffer,
315 																				 VkDeviceSize							commandSize) const;
316 	void								checkFramebufferSupport					(const VkFramebufferCreateInfo*			pCreateInfo) const;
317 	void								checkRenderPassSupport					(deUint32								attachmentCount,
318 																				 deUint32								subpassCount,
319 																				 deUint32								dependencyCount) const;
320 	void								checkSubpassSupport						(deUint32								inputAttachmentCount,
321 																				 deUint32								preserveAttachmentCount) const;
322 
323 
324 	de::SharedPtr<ResourceInterface>	gerResourceInterface					() const;
325 	void								reset									() const;
326 
327 protected:
328 	mutable std::mutex															functionMutex;
329 	bool																		m_normalMode;
330 
331 	de::SharedPtr<vk::ResourceInterface>										m_resourceInterface;
332 
333 	mutable std::vector<deUint8>												m_falseMemory;
334 	mutable std::map<VkImageView, VkImageViewCreateInfo>						m_imageViews;
335 	mutable std::map<VkDescriptorSetLayout, VkDescriptorSetLayoutCreateInfo>	m_descriptorSetLayouts;
336 	mutable std::map<VkRenderPass, VkRenderPassCreateInfo>						m_renderPasses;
337 	mutable std::map<VkRenderPass, VkRenderPassCreateInfo2>						m_renderPasses2;
338 	mutable std::map<VkPipeline, VkGraphicsPipelineCreateInfo>					m_graphicsPipelines;
339 	mutable std::map<VkPipeline, VkComputePipelineCreateInfo>					m_computePipelines;
340 	mutable std::map<VkDescriptorSet, VkDescriptorPool>							m_descriptorSetsInPool;
341 	VkPhysicalDeviceVulkanSC10Properties										m_physicalDeviceVulkanSC10Properties;
342 	VkPhysicalDeviceProperties													m_physicalDeviceProperties;
343 
344 	VkDeviceSize																m_commandDefaultSize;
345 	VkDeviceSize																m_commandBufferMinimumSize;
346 	VkDeviceSize																m_commandPoolMinimumSize;
347 };
348 
349 class DeinitDeviceDeleter : public Deleter<DeviceDriverSC>
350 {
351 public:
DeinitDeviceDeleter(ResourceInterface* resourceInterface, const VkDevice& device)352 										DeinitDeviceDeleter					(ResourceInterface* resourceInterface, const VkDevice& device)
353 		: m_resourceInterface(resourceInterface)
354 		, m_device(device)
355 	{
356 	}
DeinitDeviceDeleter(void)357 										DeinitDeviceDeleter					(void)
358 		: m_resourceInterface(DE_NULL)
359 		, m_device(DE_NULL)
360 	{}
361 
operator ()(DeviceDriverSC* obj) const362 	void								operator()							(DeviceDriverSC* obj) const
363 	{
364 		if (m_resourceInterface != DE_NULL)
365 			m_resourceInterface->deinitDevice(m_device);
366 		delete obj;
367 	}
368 private:
369 	ResourceInterface*	m_resourceInterface;
370 	VkDevice			m_device;
371 };
372 
373 #endif // CTS_USES_VULKANSC
374 
375 // Single device driver pointer type which will differ in SC and non-SC mode helping clearing the code
376 #ifndef CTS_USES_VULKANSC
377 typedef de::MovePtr<DeviceDriver> DeviceDriverPtr;
378 #else
379 typedef de::MovePtr<DeviceDriverSC, vk::DeinitDeviceDeleter> DeviceDriverPtr;
380 #endif // CTS_USES_VULKANSC
381 
382 // Defined in vkWsiPlatform.hpp
383 namespace wsi
384 {
385 class Display;
386 } // wsi
387 
388 /*--------------------------------------------------------------------*//*!
389  * \brief Vulkan platform interface
390  *//*--------------------------------------------------------------------*/
391 class Platform
392 {
393 public:
394 	enum LibraryType
395 	{
396 		LIBRARY_TYPE_VULKAN						= 0,
397 		LIBRARY_TYPE_LAST
398 	};
399 
Platform(void)400 							Platform			(void) {}
~Platform(void)401 							~Platform			(void) {}
402 #ifdef DE_PLATFORM_USE_LIBRARY_TYPE
403 	virtual Library*		createLibrary		(LibraryType libraryType = LIBRARY_TYPE_VULKAN, const char* libraryPath = DE_NULL) const = 0;
404 #else
405 	virtual Library*		createLibrary		(const char* libraryPath = DE_NULL) const = 0;
406 #endif
407 
408 	virtual wsi::Display*	createWsiDisplay	(wsi::Type wsiType) const;
409 	virtual bool			hasDisplay			(wsi::Type wsiType) const;
410 	virtual void			describePlatform	(std::ostream& dst) const;
411 };
412 
413 } // vk
414 
415 #endif // _VKPLATFORM_HPP
416