87f095c3756480d26c6990422eea23f9ca76a2e9
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / framework / vulkan / vkNullDriver.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and/or associated documentation files (the
9  * "Materials"), to deal in the Materials without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Materials, and to
12  * permit persons to whom the Materials are furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice(s) and this permission notice shall be
16  * included in all copies or substantial portions of the Materials.
17  *
18  * The Materials are Confidential Information as defined by the
19  * Khronos Membership Agreement until designated non-confidential by
20  * Khronos, at which point this condition clause shall be removed.
21  *
22  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
29  *
30  *//*!
31  * \file
32  * \brief Null (dummy) Vulkan implementation.
33  *//*--------------------------------------------------------------------*/
34
35 #include "vkNullDriver.hpp"
36 #include "vkPlatform.hpp"
37 #include "tcuFunctionLibrary.hpp"
38 #include "deMemory.h"
39
40 #include <stdexcept>
41
42 namespace vk
43 {
44
45 namespace
46 {
47
48 #define VK_NULL_RETURN(STMT)                                    \
49         do {                                                                            \
50                 try {                                                                   \
51                         STMT;                                                           \
52                         return VK_SUCCESS;                                      \
53                 } catch (const std::bad_alloc&) {               \
54                         return VK_ERROR_OUT_OF_HOST_MEMORY;     \
55                 } catch (VkResult res) {                                \
56                         return res;                                                     \
57                 }                                                                               \
58         } while (deGetFalse())
59
60 // \todo [2015-07-14 pyry] Check FUNC type by checkedCastToPtr<T>() or similar
61 #define VK_NULL_FUNC_ENTRY(NAME, FUNC)  { #NAME, (deFunctionPtr)FUNC }
62
63 #define VK_NULL_DEFINE_DEVICE_OBJ(NAME)                         \
64 struct NAME                                                                                     \
65 {                                                                                                       \
66         NAME (VkDevice, const Vk##NAME##CreateInfo*) {} \
67 }
68
69 class Instance
70 {
71 public:
72                                                                                 Instance                (const VkInstanceCreateInfo* instanceInfo);
73                                                                                 ~Instance               (void) {}
74
75         PFN_vkVoidFunction                                      getProcAddr             (const char* name) const { return (PFN_vkVoidFunction)m_functions.getFunction(name); }
76
77 private:
78         const tcu::StaticFunctionLibrary        m_functions;
79 };
80
81 class Device
82 {
83 public:
84                                                                                 Device                  (VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* deviceInfo);
85                                                                                 ~Device                 (void) {}
86
87         PFN_vkVoidFunction                                      getProcAddr             (const char* name) const { return (PFN_vkVoidFunction)m_functions.getFunction(name); }
88
89 private:
90         const tcu::StaticFunctionLibrary        m_functions;
91 };
92
93 class DescriptorSet
94 {
95 public:
96         DescriptorSet (VkDevice, VkDescriptorPool, VkDescriptorSetLayout) {}
97 };
98
99 class Pipeline
100 {
101 public:
102         Pipeline (VkDevice, const VkGraphicsPipelineCreateInfo*) {}
103         Pipeline (VkDevice, const VkComputePipelineCreateInfo*) {}
104 };
105
106 class DeviceMemory
107 {
108 public:
109                                                 DeviceMemory    (VkDevice, const VkMemoryAllocateInfo* pAllocInfo)
110                                                         : m_memory(deMalloc((size_t)pAllocInfo->allocationSize))
111                                                 {
112                                                         if (!m_memory)
113                                                                 throw std::bad_alloc();
114                                                 }
115                                                 ~DeviceMemory   (void)
116                                                 {
117                                                         deFree(m_memory);
118                                                 }
119
120         void*                           getPtr                  (void) const { return m_memory; }
121
122 private:
123         void* const                     m_memory;
124 };
125
126 class Buffer
127 {
128 public:
129                                                 Buffer          (VkDevice, const VkBufferCreateInfo* pCreateInfo)
130                                                         : m_size(pCreateInfo->size)
131                                                 {}
132
133         VkDeviceSize            getSize         (void) const { return m_size;   }
134
135 private:
136         const VkDeviceSize      m_size;
137 };
138
139 class CommandBuffer
140 {
141 public:
142                                                 CommandBuffer(VkDevice, VkCommandPool, VkCommandBufferLevel)
143                                                 {}
144 };
145
146 VK_NULL_DEFINE_DEVICE_OBJ(Fence);
147 VK_NULL_DEFINE_DEVICE_OBJ(Image);
148 VK_NULL_DEFINE_DEVICE_OBJ(Semaphore);
149 VK_NULL_DEFINE_DEVICE_OBJ(Event);
150 VK_NULL_DEFINE_DEVICE_OBJ(QueryPool);
151 VK_NULL_DEFINE_DEVICE_OBJ(BufferView);
152 VK_NULL_DEFINE_DEVICE_OBJ(ImageView);
153 VK_NULL_DEFINE_DEVICE_OBJ(ShaderModule);
154 VK_NULL_DEFINE_DEVICE_OBJ(PipelineCache);
155 VK_NULL_DEFINE_DEVICE_OBJ(PipelineLayout);
156 VK_NULL_DEFINE_DEVICE_OBJ(RenderPass);
157 VK_NULL_DEFINE_DEVICE_OBJ(DescriptorSetLayout);
158 VK_NULL_DEFINE_DEVICE_OBJ(Sampler);
159 VK_NULL_DEFINE_DEVICE_OBJ(Framebuffer);
160 VK_NULL_DEFINE_DEVICE_OBJ(CommandPool);
161 VK_NULL_DEFINE_DEVICE_OBJ(DescriptorPool);
162
163 extern "C"
164 {
165
166 PFN_vkVoidFunction getInstanceProcAddr (VkInstance instance, const char* pName)
167 {
168         return reinterpret_cast<Instance*>(instance)->getProcAddr(pName);
169 }
170
171 PFN_vkVoidFunction getDeviceProcAddr (VkDevice device, const char* pName)
172 {
173         return reinterpret_cast<Device*>(device)->getProcAddr(pName);
174 }
175
176 VkResult createGraphicsPipelines (VkDevice device, VkPipelineCache, deUint32 count, const VkGraphicsPipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks*, VkPipeline* pPipelines)
177 {
178         for (deUint32 ndx = 0; ndx < count; ndx++)
179                 pPipelines[ndx] = VkPipeline((deUint64)(deUintptr)new Pipeline(device, pCreateInfos+ndx));
180         return VK_SUCCESS;
181 }
182
183 VkResult createComputePipelines (VkDevice device, VkPipelineCache, deUint32 count, const VkComputePipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks*, VkPipeline* pPipelines)
184 {
185         for (deUint32 ndx = 0; ndx < count; ndx++)
186                 pPipelines[ndx] = VkPipeline((deUint64)(deUintptr)new Pipeline(device, pCreateInfos+ndx));
187         return VK_SUCCESS;
188 }
189
190 VkResult enumeratePhysicalDevices (VkInstance, deUint32* pPhysicalDeviceCount, VkPhysicalDevice* pDevices)
191 {
192         if (pDevices && *pPhysicalDeviceCount >= 1u)
193                 *pDevices = reinterpret_cast<VkPhysicalDevice>((void*)(deUintptr)1u);
194
195         *pPhysicalDeviceCount = 1;
196
197         return VK_SUCCESS;
198 }
199
200 VkResult getPhysicalDeviceProperties (VkPhysicalDevice, VkPhysicalDeviceProperties* props)
201 {
202         deMemset(props, 0, sizeof(VkPhysicalDeviceProperties));
203
204         props->apiVersion               = VK_API_VERSION;
205         props->driverVersion    = 1u;
206         props->deviceType               = VK_PHYSICAL_DEVICE_TYPE_OTHER;
207
208         deMemcpy(props->deviceName, "null", 5);
209
210         // \todo [2015-09-25 pyry] Fill in reasonable limits
211
212         return VK_SUCCESS;
213 }
214
215 VkResult getPhysicalDeviceQueueFamilyProperties (VkPhysicalDevice, deUint32* count, VkQueueFamilyProperties* props)
216 {
217         if (props && *count >= 1u)
218         {
219                 deMemset(props, 0, sizeof(VkQueueFamilyProperties));
220
221                 props->queueCount                       = 1u;
222                 props->queueFlags                       = VK_QUEUE_GRAPHICS_BIT|VK_QUEUE_COMPUTE_BIT;
223                 props->timestampValidBits       = 64;
224         }
225
226         *count = 1u;
227
228         return VK_SUCCESS;
229 }
230
231 VkResult getPhysicalDeviceMemoryProperties (VkPhysicalDevice, VkPhysicalDeviceMemoryProperties* props)
232 {
233         deMemset(props, 0, sizeof(VkPhysicalDeviceMemoryProperties));
234
235         props->memoryTypeCount                          = 1u;
236         props->memoryTypes[0].heapIndex         = 0u;
237         props->memoryTypes[0].propertyFlags     = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
238
239         props->memoryHeapCount                          = 1u;
240         props->memoryHeaps[0].size                      = 1ull << 31;
241         props->memoryHeaps[0].flags                     = 0u;
242
243         return VK_SUCCESS;
244 }
245
246 VkResult getPhysicalDeviceFormatProperties (VkPhysicalDevice, VkFormat, VkFormatProperties* pFormatProperties)
247 {
248         const VkFormatFeatureFlags      allFeatures     = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
249                                                                                         | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
250                                                                                         | VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
251                                                                                         | VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT
252                                                                                         | VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT
253                                                                                         | VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT
254                                                                                         | VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT
255                                                                                         | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
256                                                                                         | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
257                                                                                         | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
258                                                                                         | VK_FORMAT_FEATURE_BLIT_SRC_BIT
259                                                                                         | VK_FORMAT_FEATURE_BLIT_DST_BIT;
260
261         pFormatProperties->linearTilingFeatures         = allFeatures;
262         pFormatProperties->optimalTilingFeatures        = allFeatures;
263         pFormatProperties->bufferFeatures                       = allFeatures;
264
265         return VK_SUCCESS;
266 }
267
268 VkResult getBufferMemoryRequirements (VkDevice, VkBuffer bufferHandle, VkMemoryRequirements* requirements)
269 {
270         const Buffer*   buffer  = reinterpret_cast<Buffer*>(bufferHandle.getInternal());
271
272         requirements->memoryTypeBits    = 1u;
273         requirements->size                              = buffer->getSize();
274         requirements->alignment                 = (VkDeviceSize)1u;
275
276         return VK_SUCCESS;
277 }
278
279 VkResult getImageMemoryRequirements (VkDevice, VkImage, VkMemoryRequirements* requirements)
280 {
281         requirements->memoryTypeBits    = 1u;
282         requirements->size                              = 4u;
283         requirements->alignment                 = 4u;
284
285         return VK_SUCCESS;
286 }
287
288 VkResult mapMemory (VkDevice, VkDeviceMemory memHandle, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData)
289 {
290         const DeviceMemory*     memory  = reinterpret_cast<DeviceMemory*>(memHandle.getInternal());
291
292         DE_UNREF(size);
293         DE_UNREF(flags);
294
295         *ppData = (deUint8*)memory->getPtr() + offset;
296
297         return VK_SUCCESS;
298 }
299
300 VkResult allocateDescriptorSets (VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pDescriptorSets)
301 {
302         for (deUint32 ndx = 0; ndx < pAllocateInfo->setLayoutCount; ++ndx)
303         {
304                 try
305                 {
306                         pDescriptorSets[ndx] = VkDescriptorSet((deUint64)(deUintptr)new DescriptorSet(device, pAllocateInfo->descriptorPool, pAllocateInfo->pSetLayouts[ndx]));
307                 }
308                 catch (const std::bad_alloc&)
309                 {
310                         for (deUint32 freeNdx = 0; freeNdx < ndx; freeNdx++)
311                                 delete reinterpret_cast<DescriptorSet*>((deUintptr)pDescriptorSets[freeNdx].getInternal());
312
313                         return VK_ERROR_OUT_OF_HOST_MEMORY;
314                 }
315                 catch (VkResult res)
316                 {
317                         for (deUint32 freeNdx = 0; freeNdx < ndx; freeNdx++)
318                                 delete reinterpret_cast<DescriptorSet*>((deUintptr)pDescriptorSets[freeNdx].getInternal());
319
320                         return res;
321                 }
322         }
323
324         return VK_SUCCESS;
325 }
326
327 void freeDescriptorSets (VkDevice, VkDescriptorPool, deUint32 count, const VkDescriptorSet* pDescriptorSets)
328 {
329         for (deUint32 ndx = 0; ndx < count; ++ndx)
330         {
331                 // \note: delete cannot fail
332                 delete reinterpret_cast<DescriptorSet*>((deUintptr)pDescriptorSets[ndx].getInternal());
333         }
334 }
335
336 VkResult allocateCommandBuffers (VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers)
337 {
338         if (pAllocateInfo && pCommandBuffers)
339         {
340                 for (deUint32 ndx = 0; ndx < pAllocateInfo->bufferCount; ++ndx)
341                 {
342                         pCommandBuffers[ndx] = reinterpret_cast<VkCommandBuffer>(new CommandBuffer(device, pAllocateInfo->commandPool, pAllocateInfo->level));
343                 }
344         }
345
346         return VK_SUCCESS;
347 }
348
349 void freeCommandBuffers (VkDevice device, VkCommandPool commandPool, deUint32 commandBufferCount, const VkCommandBuffer* pCommandBuffers)
350 {
351         DE_UNREF(device);
352         DE_UNREF(commandPool);
353
354         for (deUint32 ndx = 0; ndx < commandBufferCount; ++ndx)
355                 delete reinterpret_cast<CommandBuffer*>(pCommandBuffers[ndx]);
356 }
357
358 #include "vkNullDriverImpl.inl"
359
360 } // extern "C"
361
362 Instance::Instance (const VkInstanceCreateInfo*)
363         : m_functions(s_instanceFunctions, DE_LENGTH_OF_ARRAY(s_instanceFunctions))
364 {
365 }
366
367 Device::Device (VkPhysicalDevice, const VkDeviceCreateInfo*)
368         : m_functions(s_deviceFunctions, DE_LENGTH_OF_ARRAY(s_deviceFunctions))
369 {
370 }
371
372 class NullDriverLibrary : public Library
373 {
374 public:
375                                                                 NullDriverLibrary (void)
376                                                                         : m_library     (s_platformFunctions, DE_LENGTH_OF_ARRAY(s_platformFunctions))
377                                                                         , m_driver      (m_library)
378                                                                 {}
379
380         const PlatformInterface&        getPlatformInterface    (void) const { return m_driver; }
381
382 private:
383         const tcu::StaticFunctionLibrary        m_library;
384         const PlatformDriver                            m_driver;
385 };
386
387 } // anonymous
388
389 Library* createNullDriver (void)
390 {
391         return new NullDriverLibrary();
392 }
393
394 } // vk