1 /*-------------------------------------------------------------------------
5 * Copyright (c) 2015 Google Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Null (dummy) Vulkan implementation.
22 *//*--------------------------------------------------------------------*/
24 #include "vkNullDriver.hpp"
25 #include "vkPlatform.hpp"
26 #include "vkImageUtil.hpp"
27 #include "tcuFunctionLibrary.hpp"
44 void* allocateSystemMem (const VkAllocationCallbacks* pAllocator, VkSystemAllocationScope scope)
46 void* ptr = pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(T), sizeof(void*), scope);
48 throw std::bad_alloc();
52 void freeSystemMem (const VkAllocationCallbacks* pAllocator, void* mem)
54 pAllocator->pfnFree(pAllocator->pUserData, mem);
57 template<typename Object, typename Handle, typename Parent, typename CreateInfo>
58 Handle allocateHandle (Parent parent, const CreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator)
60 Object* obj = DE_NULL;
64 void* mem = allocateSystemMem<Object>(pAllocator, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
67 obj = new (mem) Object(parent, pCreateInfo);
68 DE_ASSERT(obj == mem);
72 pAllocator->pfnFree(pAllocator->pUserData, mem);
77 obj = new Object(parent, pCreateInfo);
79 return reinterpret_cast<Handle>(obj);
82 template<typename Object, typename Handle, typename CreateInfo>
83 Handle allocateHandle (const CreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator)
85 Object* obj = DE_NULL;
89 void* mem = allocateSystemMem<Object>(pAllocator, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
92 obj = new (mem) Object(pCreateInfo);
93 DE_ASSERT(obj == mem);
97 pAllocator->pfnFree(pAllocator->pUserData, mem);
102 obj = new Object(pCreateInfo);
104 return reinterpret_cast<Handle>(obj);
107 template<typename Object, typename Handle>
108 void freeHandle (Handle handle, const VkAllocationCallbacks* pAllocator)
110 Object* obj = reinterpret_cast<Object*>(handle);
115 freeSystemMem(pAllocator, reinterpret_cast<void*>(obj));
121 template<typename Object, typename Handle, typename Parent, typename CreateInfo>
122 Handle allocateNonDispHandle (Parent parent, const CreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator)
124 Object* const obj = allocateHandle<Object, Object*>(parent, pCreateInfo, pAllocator);
125 return Handle((deUint64)(deUintptr)obj);
128 template<typename Object, typename Handle>
129 void freeNonDispHandle (Handle handle, const VkAllocationCallbacks* pAllocator)
131 freeHandle<Object>(reinterpret_cast<Object*>((deUintptr)handle.getInternal()), pAllocator);
134 // Object definitions
136 #define VK_NULL_RETURN(STMT) \
141 } catch (const std::bad_alloc&) { \
142 return VK_ERROR_OUT_OF_HOST_MEMORY; \
143 } catch (VkResult res) { \
146 } while (deGetFalse())
148 // \todo [2015-07-14 pyry] Check FUNC type by checkedCastToPtr<T>() or similar
149 #define VK_NULL_FUNC_ENTRY(NAME, FUNC) { #NAME, (deFunctionPtr)FUNC }
151 #define VK_NULL_DEFINE_DEVICE_OBJ(NAME) \
154 NAME (VkDevice, const Vk##NAME##CreateInfo*) {} \
157 VK_NULL_DEFINE_DEVICE_OBJ(Fence);
158 VK_NULL_DEFINE_DEVICE_OBJ(Semaphore);
159 VK_NULL_DEFINE_DEVICE_OBJ(Event);
160 VK_NULL_DEFINE_DEVICE_OBJ(QueryPool);
161 VK_NULL_DEFINE_DEVICE_OBJ(BufferView);
162 VK_NULL_DEFINE_DEVICE_OBJ(ImageView);
163 VK_NULL_DEFINE_DEVICE_OBJ(ShaderModule);
164 VK_NULL_DEFINE_DEVICE_OBJ(PipelineCache);
165 VK_NULL_DEFINE_DEVICE_OBJ(PipelineLayout);
166 VK_NULL_DEFINE_DEVICE_OBJ(RenderPass);
167 VK_NULL_DEFINE_DEVICE_OBJ(DescriptorSetLayout);
168 VK_NULL_DEFINE_DEVICE_OBJ(Sampler);
169 VK_NULL_DEFINE_DEVICE_OBJ(Framebuffer);
170 VK_NULL_DEFINE_DEVICE_OBJ(CommandPool);
175 Instance (const VkInstanceCreateInfo* instanceInfo);
178 PFN_vkVoidFunction getProcAddr (const char* name) const { return (PFN_vkVoidFunction)m_functions.getFunction(name); }
181 const tcu::StaticFunctionLibrary m_functions;
187 SurfaceKHR (VkInstance, const VkXlibSurfaceCreateInfoKHR*) {}
188 SurfaceKHR (VkInstance, const VkXcbSurfaceCreateInfoKHR*) {}
189 SurfaceKHR (VkInstance, const VkWaylandSurfaceCreateInfoKHR*) {}
190 SurfaceKHR (VkInstance, const VkMirSurfaceCreateInfoKHR*) {}
191 SurfaceKHR (VkInstance, const VkAndroidSurfaceCreateInfoKHR*) {}
192 SurfaceKHR (VkInstance, const VkWin32SurfaceCreateInfoKHR*) {}
193 SurfaceKHR (VkInstance, const VkDisplaySurfaceCreateInfoKHR*) {}
194 ~SurfaceKHR (void) {}
200 DisplayModeKHR (VkDisplayKHR, const VkDisplayModeCreateInfoKHR*) {}
201 ~DisplayModeKHR (void) {}
204 class DebugReportCallbackEXT
207 DebugReportCallbackEXT (VkInstance, const VkDebugReportCallbackCreateInfoEXT*) {}
208 ~DebugReportCallbackEXT (void) {}
214 Device (VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* deviceInfo);
217 PFN_vkVoidFunction getProcAddr (const char* name) const { return (PFN_vkVoidFunction)m_functions.getFunction(name); }
220 const tcu::StaticFunctionLibrary m_functions;
226 Pipeline (VkDevice, const VkGraphicsPipelineCreateInfo*) {}
227 Pipeline (VkDevice, const VkComputePipelineCreateInfo*) {}
233 SwapchainKHR (VkDevice, const VkSwapchainCreateInfoKHR*) {}
234 ~SwapchainKHR (void) {}
237 void* allocateHeap (const VkMemoryAllocateInfo* pAllocInfo)
239 // \todo [2015-12-03 pyry] Alignment requirements?
240 // \todo [2015-12-03 pyry] Empty allocations okay?
241 if (pAllocInfo->allocationSize > 0)
243 void* const heapPtr = deMalloc((size_t)pAllocInfo->allocationSize);
245 throw std::bad_alloc();
252 void freeHeap (void* ptr)
260 DeviceMemory (VkDevice, const VkMemoryAllocateInfo* pAllocInfo)
261 : m_memory(allocateHeap(pAllocInfo))
269 void* getPtr (void) const { return m_memory; }
272 void* const m_memory;
278 Buffer (VkDevice, const VkBufferCreateInfo* pCreateInfo)
279 : m_size(pCreateInfo->size)
282 VkDeviceSize getSize (void) const { return m_size; }
285 const VkDeviceSize m_size;
291 Image (VkDevice, const VkImageCreateInfo* pCreateInfo)
292 : m_imageType (pCreateInfo->imageType)
293 , m_format (pCreateInfo->format)
294 , m_extent (pCreateInfo->extent)
295 , m_samples (pCreateInfo->samples)
298 VkImageType getImageType (void) const { return m_imageType; }
299 VkFormat getFormat (void) const { return m_format; }
300 VkExtent3D getExtent (void) const { return m_extent; }
301 VkSampleCountFlagBits getSamples (void) const { return m_samples; }
304 const VkImageType m_imageType;
305 const VkFormat m_format;
306 const VkExtent3D m_extent;
307 const VkSampleCountFlagBits m_samples;
313 CommandBuffer(VkDevice, VkCommandPool, VkCommandBufferLevel)
320 DescriptorSet (VkDevice, VkDescriptorPool, VkDescriptorSetLayout) {}
326 DescriptorPool (VkDevice device, const VkDescriptorPoolCreateInfo* pCreateInfo)
328 , m_flags (pCreateInfo->flags)
330 ~DescriptorPool (void)
335 VkDescriptorSet allocate (VkDescriptorSetLayout setLayout);
336 void free (VkDescriptorSet set);
341 const VkDevice m_device;
342 const VkDescriptorPoolCreateFlags m_flags;
344 vector<DescriptorSet*> m_managedSets;
347 VkDescriptorSet DescriptorPool::allocate (VkDescriptorSetLayout setLayout)
349 DescriptorSet* const impl = new DescriptorSet(m_device, VkDescriptorPool(reinterpret_cast<deUintptr>(this)), setLayout);
353 m_managedSets.push_back(impl);
361 return VkDescriptorSet(reinterpret_cast<deUintptr>(impl));
364 void DescriptorPool::free (VkDescriptorSet set)
366 DescriptorSet* const impl = reinterpret_cast<DescriptorSet*>((deUintptr)set.getInternal());
368 DE_ASSERT(m_flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
372 for (size_t ndx = 0; ndx < m_managedSets.size(); ++ndx)
374 if (m_managedSets[ndx] == impl)
376 std::swap(m_managedSets[ndx], m_managedSets.back());
377 m_managedSets.pop_back();
382 DE_FATAL("VkDescriptorSet not owned by VkDescriptorPool");
385 void DescriptorPool::reset (void)
387 for (size_t ndx = 0; ndx < m_managedSets.size(); ++ndx)
388 delete m_managedSets[ndx];
389 m_managedSets.clear();
392 // API implementation
397 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL getInstanceProcAddr (VkInstance instance, const char* pName)
399 return reinterpret_cast<Instance*>(instance)->getProcAddr(pName);
402 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL getDeviceProcAddr (VkDevice device, const char* pName)
404 return reinterpret_cast<Device*>(device)->getProcAddr(pName);
407 VKAPI_ATTR VkResult VKAPI_CALL createGraphicsPipelines (VkDevice device, VkPipelineCache, deUint32 count, const VkGraphicsPipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines)
412 for (allocNdx = 0; allocNdx < count; allocNdx++)
413 pPipelines[allocNdx] = allocateNonDispHandle<Pipeline, VkPipeline>(device, pCreateInfos+allocNdx, pAllocator);
417 catch (const std::bad_alloc&)
419 for (deUint32 freeNdx = 0; freeNdx < allocNdx; freeNdx++)
420 freeNonDispHandle<Pipeline, VkPipeline>(pPipelines[freeNdx], pAllocator);
422 return VK_ERROR_OUT_OF_HOST_MEMORY;
426 for (deUint32 freeNdx = 0; freeNdx < allocNdx; freeNdx++)
427 freeNonDispHandle<Pipeline, VkPipeline>(pPipelines[freeNdx], pAllocator);
433 VKAPI_ATTR VkResult VKAPI_CALL createComputePipelines (VkDevice device, VkPipelineCache, deUint32 count, const VkComputePipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines)
438 for (allocNdx = 0; allocNdx < count; allocNdx++)
439 pPipelines[allocNdx] = allocateNonDispHandle<Pipeline, VkPipeline>(device, pCreateInfos+allocNdx, pAllocator);
443 catch (const std::bad_alloc&)
445 for (deUint32 freeNdx = 0; freeNdx < allocNdx; freeNdx++)
446 freeNonDispHandle<Pipeline, VkPipeline>(pPipelines[freeNdx], pAllocator);
448 return VK_ERROR_OUT_OF_HOST_MEMORY;
452 for (deUint32 freeNdx = 0; freeNdx < allocNdx; freeNdx++)
453 freeNonDispHandle<Pipeline, VkPipeline>(pPipelines[freeNdx], pAllocator);
459 VKAPI_ATTR VkResult VKAPI_CALL enumeratePhysicalDevices (VkInstance, deUint32* pPhysicalDeviceCount, VkPhysicalDevice* pDevices)
461 if (pDevices && *pPhysicalDeviceCount >= 1u)
462 *pDevices = reinterpret_cast<VkPhysicalDevice>((void*)(deUintptr)1u);
464 *pPhysicalDeviceCount = 1;
469 VKAPI_ATTR void VKAPI_CALL getPhysicalDeviceProperties (VkPhysicalDevice, VkPhysicalDeviceProperties* props)
471 deMemset(props, 0, sizeof(VkPhysicalDeviceProperties));
473 props->apiVersion = VK_API_VERSION;
474 props->driverVersion = 1u;
475 props->deviceType = VK_PHYSICAL_DEVICE_TYPE_OTHER;
477 deMemcpy(props->deviceName, "null", 5);
479 // \todo [2015-09-25 pyry] Fill in reasonable limits
480 props->limits.maxTexelBufferElements = 8096;
483 VKAPI_ATTR void VKAPI_CALL getPhysicalDeviceQueueFamilyProperties (VkPhysicalDevice, deUint32* count, VkQueueFamilyProperties* props)
485 if (props && *count >= 1u)
487 deMemset(props, 0, sizeof(VkQueueFamilyProperties));
489 props->queueCount = 1u;
490 props->queueFlags = VK_QUEUE_GRAPHICS_BIT|VK_QUEUE_COMPUTE_BIT;
491 props->timestampValidBits = 64;
497 VKAPI_ATTR void VKAPI_CALL getPhysicalDeviceMemoryProperties (VkPhysicalDevice, VkPhysicalDeviceMemoryProperties* props)
499 deMemset(props, 0, sizeof(VkPhysicalDeviceMemoryProperties));
501 props->memoryTypeCount = 1u;
502 props->memoryTypes[0].heapIndex = 0u;
503 props->memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
505 props->memoryHeapCount = 1u;
506 props->memoryHeaps[0].size = 1ull << 31;
507 props->memoryHeaps[0].flags = 0u;
510 VKAPI_ATTR void VKAPI_CALL getPhysicalDeviceFormatProperties (VkPhysicalDevice, VkFormat, VkFormatProperties* pFormatProperties)
512 const VkFormatFeatureFlags allFeatures = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
513 | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
514 | VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
515 | VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT
516 | VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT
517 | VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT
518 | VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT
519 | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
520 | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
521 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
522 | VK_FORMAT_FEATURE_BLIT_SRC_BIT
523 | VK_FORMAT_FEATURE_BLIT_DST_BIT;
525 pFormatProperties->linearTilingFeatures = allFeatures;
526 pFormatProperties->optimalTilingFeatures = allFeatures;
527 pFormatProperties->bufferFeatures = allFeatures;
530 VKAPI_ATTR void VKAPI_CALL getBufferMemoryRequirements (VkDevice, VkBuffer bufferHandle, VkMemoryRequirements* requirements)
532 const Buffer* buffer = reinterpret_cast<const Buffer*>(bufferHandle.getInternal());
534 requirements->memoryTypeBits = 1u;
535 requirements->size = buffer->getSize();
536 requirements->alignment = (VkDeviceSize)1u;
539 VkDeviceSize getPackedImageDataSize (VkFormat format, VkExtent3D extent, VkSampleCountFlagBits samples)
541 return (VkDeviceSize)getPixelSize(mapVkFormat(format))
542 * (VkDeviceSize)extent.width
543 * (VkDeviceSize)extent.height
544 * (VkDeviceSize)extent.depth
545 * (VkDeviceSize)samples;
548 VkDeviceSize getCompressedImageDataSize (VkFormat format, VkExtent3D extent)
552 const tcu::CompressedTexFormat tcuFormat = mapVkCompressedFormat(format);
553 const size_t blockSize = tcu::getBlockSize(tcuFormat);
554 const tcu::IVec3 blockPixelSize = tcu::getBlockPixelSize(tcuFormat);
555 const int numBlocksX = deDivRoundUp32((int)extent.width, blockPixelSize.x());
556 const int numBlocksY = deDivRoundUp32((int)extent.height, blockPixelSize.y());
557 const int numBlocksZ = deDivRoundUp32((int)extent.depth, blockPixelSize.z());
559 return blockSize*numBlocksX*numBlocksY*numBlocksZ;
563 return 0; // Unsupported compressed format
567 VKAPI_ATTR void VKAPI_CALL getImageMemoryRequirements (VkDevice, VkImage imageHandle, VkMemoryRequirements* requirements)
569 const Image* image = reinterpret_cast<const Image*>(imageHandle.getInternal());
571 requirements->memoryTypeBits = 1u;
572 requirements->alignment = 16u;
574 if (isCompressedFormat(image->getFormat()))
575 requirements->size = getCompressedImageDataSize(image->getFormat(), image->getExtent());
577 requirements->size = getPackedImageDataSize(image->getFormat(), image->getExtent(), image->getSamples());
580 VKAPI_ATTR VkResult VKAPI_CALL mapMemory (VkDevice, VkDeviceMemory memHandle, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData)
582 const DeviceMemory* memory = reinterpret_cast<DeviceMemory*>(memHandle.getInternal());
587 *ppData = (deUint8*)memory->getPtr() + offset;
592 VKAPI_ATTR VkResult VKAPI_CALL allocateDescriptorSets (VkDevice, const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pDescriptorSets)
594 DescriptorPool* const poolImpl = reinterpret_cast<DescriptorPool*>((deUintptr)pAllocateInfo->descriptorPool.getInternal());
596 for (deUint32 ndx = 0; ndx < pAllocateInfo->descriptorSetCount; ++ndx)
600 pDescriptorSets[ndx] = poolImpl->allocate(pAllocateInfo->pSetLayouts[ndx]);
602 catch (const std::bad_alloc&)
604 for (deUint32 freeNdx = 0; freeNdx < ndx; freeNdx++)
605 delete reinterpret_cast<DescriptorSet*>((deUintptr)pDescriptorSets[freeNdx].getInternal());
607 return VK_ERROR_OUT_OF_HOST_MEMORY;
611 for (deUint32 freeNdx = 0; freeNdx < ndx; freeNdx++)
612 delete reinterpret_cast<DescriptorSet*>((deUintptr)pDescriptorSets[freeNdx].getInternal());
621 VKAPI_ATTR void VKAPI_CALL freeDescriptorSets (VkDevice, VkDescriptorPool descriptorPool, deUint32 count, const VkDescriptorSet* pDescriptorSets)
623 DescriptorPool* const poolImpl = reinterpret_cast<DescriptorPool*>((deUintptr)descriptorPool.getInternal());
625 for (deUint32 ndx = 0; ndx < count; ++ndx)
626 poolImpl->free(pDescriptorSets[ndx]);
629 VKAPI_ATTR VkResult VKAPI_CALL resetDescriptorPool (VkDevice, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags)
631 DescriptorPool* const poolImpl = reinterpret_cast<DescriptorPool*>((deUintptr)descriptorPool.getInternal());
638 VKAPI_ATTR VkResult VKAPI_CALL allocateCommandBuffers (VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers)
640 if (pAllocateInfo && pCommandBuffers)
642 for (deUint32 ndx = 0; ndx < pAllocateInfo->commandBufferCount; ++ndx)
644 pCommandBuffers[ndx] = reinterpret_cast<VkCommandBuffer>(new CommandBuffer(device, pAllocateInfo->commandPool, pAllocateInfo->level));
651 VKAPI_ATTR void VKAPI_CALL freeCommandBuffers (VkDevice device, VkCommandPool commandPool, deUint32 commandBufferCount, const VkCommandBuffer* pCommandBuffers)
654 DE_UNREF(commandPool);
656 for (deUint32 ndx = 0; ndx < commandBufferCount; ++ndx)
657 delete reinterpret_cast<CommandBuffer*>(pCommandBuffers[ndx]);
661 VKAPI_ATTR VkResult VKAPI_CALL createDisplayModeKHR (VkPhysicalDevice, VkDisplayKHR display, const VkDisplayModeCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDisplayModeKHR* pMode)
663 DE_UNREF(pAllocator);
664 VK_NULL_RETURN((*pMode = allocateNonDispHandle<DisplayModeKHR, VkDisplayModeKHR>(display, pCreateInfo, pAllocator)));
667 VKAPI_ATTR VkResult VKAPI_CALL createSharedSwapchainsKHR (VkDevice device, deUint32 swapchainCount, const VkSwapchainCreateInfoKHR* pCreateInfos, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchains)
669 for (deUint32 ndx = 0; ndx < swapchainCount; ++ndx)
671 pSwapchains[ndx] = allocateNonDispHandle<SwapchainKHR, VkSwapchainKHR>(device, pCreateInfos+ndx, pAllocator);
677 #include "vkNullDriverImpl.inl"
681 Instance::Instance (const VkInstanceCreateInfo*)
682 : m_functions(s_instanceFunctions, DE_LENGTH_OF_ARRAY(s_instanceFunctions))
686 Device::Device (VkPhysicalDevice, const VkDeviceCreateInfo*)
687 : m_functions(s_deviceFunctions, DE_LENGTH_OF_ARRAY(s_deviceFunctions))
691 class NullDriverLibrary : public Library
694 NullDriverLibrary (void)
695 : m_library (s_platformFunctions, DE_LENGTH_OF_ARRAY(s_platformFunctions))
696 , m_driver (m_library)
699 const PlatformInterface& getPlatformInterface (void) const { return m_driver; }
702 const tcu::StaticFunctionLibrary m_library;
703 const PlatformDriver m_driver;
708 Library* createNullDriver (void)
710 return new NullDriverLibrary();