Path: blob/21.2-virgl/src/vulkan/util/vk_device.c
7132 views
/*1* Copyright © 2020 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "vk_device.h"2425#include "vk_common_entrypoints.h"26#include "vk_instance.h"27#include "vk_physical_device.h"28#include "util/hash_table.h"29#include "util/ralloc.h"3031VkResult32vk_device_init(struct vk_device *device,33struct vk_physical_device *physical_device,34const struct vk_device_dispatch_table *dispatch_table,35const VkDeviceCreateInfo *pCreateInfo,36const VkAllocationCallbacks *alloc)37{38memset(device, 0, sizeof(*device));39vk_object_base_init(device, &device->base, VK_OBJECT_TYPE_DEVICE);40if (alloc != NULL)41device->alloc = *alloc;42else43device->alloc = physical_device->instance->alloc;4445device->physical = physical_device;4647device->dispatch_table = *dispatch_table;4849/* Add common entrypoints without overwriting driver-provided ones. */50vk_device_dispatch_table_from_entrypoints(51&device->dispatch_table, &vk_common_device_entrypoints, false);5253for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {54int idx;55for (idx = 0; idx < VK_DEVICE_EXTENSION_COUNT; idx++) {56if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],57vk_device_extensions[idx].extensionName) == 0)58break;59}6061if (idx >= VK_DEVICE_EXTENSION_COUNT)62return VK_ERROR_EXTENSION_NOT_PRESENT;6364if (!physical_device->supported_extensions.extensions[idx])65return VK_ERROR_EXTENSION_NOT_PRESENT;6667#ifdef ANDROID68if (!vk_android_allowed_device_extensions.extensions[idx])69return VK_ERROR_EXTENSION_NOT_PRESENT;70#endif7172device->enabled_extensions.extensions[idx] = true;73}7475p_atomic_set(&device->private_data_next_index, 0);7677#ifdef ANDROID78mtx_init(&device->swapchain_private_mtx, mtx_plain);79device->swapchain_private = NULL;80#endif /* ANDROID */8182return VK_SUCCESS;83}8485void86vk_device_finish(UNUSED struct vk_device *device)87{88#ifdef ANDROID89if (device->swapchain_private) {90hash_table_foreach(device->swapchain_private, entry)91util_sparse_array_finish(entry->data);92ralloc_free(device->swapchain_private);93}94#endif /* ANDROID */9596vk_object_base_finish(&device->base);97}9899PFN_vkVoidFunction100vk_device_get_proc_addr(const struct vk_device *device,101const char *name)102{103if (device == NULL || name == NULL)104return NULL;105106struct vk_instance *instance = device->physical->instance;107return vk_device_dispatch_table_get_if_supported(&device->dispatch_table,108name,109instance->app_info.api_version,110&instance->enabled_extensions,111&device->enabled_extensions);112}113114VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL115vk_common_GetDeviceProcAddr(VkDevice _device,116const char *pName)117{118VK_FROM_HANDLE(vk_device, device, _device);119return vk_device_get_proc_addr(device, pName);120}121122VKAPI_ATTR void VKAPI_CALL123vk_common_GetDeviceQueue(VkDevice _device,124uint32_t queueFamilyIndex,125uint32_t queueIndex,126VkQueue *pQueue)127{128VK_FROM_HANDLE(vk_device, device, _device);129130const VkDeviceQueueInfo2 info = {131.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,132.pNext = NULL,133/* flags = 0 because (Vulkan spec 1.2.170 - vkGetDeviceQueue):134*135* "vkGetDeviceQueue must only be used to get queues that were136* created with the flags parameter of VkDeviceQueueCreateInfo set137* to zero. To get queues that were created with a non-zero flags138* parameter use vkGetDeviceQueue2."139*/140.flags = 0,141.queueFamilyIndex = queueFamilyIndex,142.queueIndex = queueIndex,143};144145device->dispatch_table.GetDeviceQueue2(_device, &info, pQueue);146}147148VKAPI_ATTR void VKAPI_CALL149vk_common_GetBufferMemoryRequirements(VkDevice _device,150VkBuffer buffer,151VkMemoryRequirements *pMemoryRequirements)152{153VK_FROM_HANDLE(vk_device, device, _device);154155VkBufferMemoryRequirementsInfo2 info = {156.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,157.buffer = buffer,158};159VkMemoryRequirements2 reqs = {160.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,161};162device->dispatch_table.GetBufferMemoryRequirements2(_device, &info, &reqs);163164*pMemoryRequirements = reqs.memoryRequirements;165}166167VKAPI_ATTR VkResult VKAPI_CALL168vk_common_BindBufferMemory(VkDevice _device,169VkBuffer buffer,170VkDeviceMemory memory,171VkDeviceSize memoryOffset)172{173VK_FROM_HANDLE(vk_device, device, _device);174175VkBindBufferMemoryInfo bind = {176.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,177.buffer = buffer,178.memory = memory,179.memoryOffset = memoryOffset,180};181182return device->dispatch_table.BindBufferMemory2(_device, 1, &bind);183}184185VKAPI_ATTR void VKAPI_CALL186vk_common_GetImageMemoryRequirements(VkDevice _device,187VkImage image,188VkMemoryRequirements *pMemoryRequirements)189{190VK_FROM_HANDLE(vk_device, device, _device);191192VkImageMemoryRequirementsInfo2 info = {193.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,194.image = image,195};196VkMemoryRequirements2 reqs = {197.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,198};199device->dispatch_table.GetImageMemoryRequirements2(_device, &info, &reqs);200201*pMemoryRequirements = reqs.memoryRequirements;202}203204VKAPI_ATTR VkResult VKAPI_CALL205vk_common_BindImageMemory(VkDevice _device,206VkImage image,207VkDeviceMemory memory,208VkDeviceSize memoryOffset)209{210VK_FROM_HANDLE(vk_device, device, _device);211212VkBindImageMemoryInfo bind = {213.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,214.image = image,215.memory = memory,216.memoryOffset = memoryOffset,217};218219return device->dispatch_table.BindImageMemory2(_device, 1, &bind);220}221222223