From d360a996f9197113cb267d651cfee1166ba3e385 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Sat, 23 Jan 2021 13:06:02 -0600 Subject: [PATCH] vulkan: Add common instance and physical device structs Reviewed-by: Lionel Landwerlin Reviewed-by: Bas Nieuwenhuizen Part-of: --- src/amd/compiler/meson.build | 2 +- src/amd/vulkan/radv_device.c | 2 +- src/broadcom/vulkan/v3dv_device.c | 2 +- src/freedreno/vulkan/tu_device.c | 2 +- src/gallium/frontends/lavapipe/lvp_device.c | 2 +- src/intel/Android.vulkan.mk | 1 + src/intel/vulkan/anv_device.c | 2 +- src/vulkan/Makefile.sources | 4 ++ src/vulkan/util/meson.build | 4 ++ src/vulkan/util/vk_device.c | 30 +++++++++- src/vulkan/util/vk_device.h | 9 +++ src/vulkan/util/vk_instance.c | 90 +++++++++++++++++++++++++++++ src/vulkan/util/vk_instance.h | 66 +++++++++++++++++++++ src/vulkan/util/vk_physical_device.c | 49 ++++++++++++++++ src/vulkan/util/vk_physical_device.h | 56 ++++++++++++++++++ 15 files changed, 314 insertions(+), 7 deletions(-) create mode 100644 src/vulkan/util/vk_instance.c create mode 100644 src/vulkan/util/vk_instance.h create mode 100644 src/vulkan/util/vk_physical_device.c create mode 100644 src/vulkan/util/vk_physical_device.h diff --git a/src/amd/compiler/meson.build b/src/amd/compiler/meson.build index 861848c..22179a0 100644 --- a/src/amd/compiler/meson.build +++ b/src/amd/compiler/meson.build @@ -99,7 +99,7 @@ _libaco = static_library( ], dependencies : [ dep_llvm, dep_thread, dep_elf, dep_libdrm_amdgpu, dep_valgrind, - idep_nir_headers, idep_amdgfxregs_h, + idep_nir_headers, idep_amdgfxregs_h, idep_vulkan_util_headers, ], gnu_symbol_visibility : 'hidden', build_by_default : true, diff --git a/src/amd/vulkan/radv_device.c b/src/amd/vulkan/radv_device.c index b2e002f..16455d8 100644 --- a/src/amd/vulkan/radv_device.c +++ b/src/amd/vulkan/radv_device.c @@ -2798,7 +2798,7 @@ VkResult radv_CreateDevice( if (!device) return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY); - result = vk_device_init(&device->vk, pCreateInfo, + result = vk_device_init(&device->vk, NULL, NULL, pCreateInfo, &physical_device->instance->alloc, pAllocator); if (result != VK_SUCCESS) { vk_free(&device->vk.alloc, device); diff --git a/src/broadcom/vulkan/v3dv_device.c b/src/broadcom/vulkan/v3dv_device.c index 7e67cb1..63eacba 100644 --- a/src/broadcom/vulkan/v3dv_device.c +++ b/src/broadcom/vulkan/v3dv_device.c @@ -1579,7 +1579,7 @@ v3dv_CreateDevice(VkPhysicalDevice physicalDevice, if (!device) return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY); - result = vk_device_init(&device->vk, pCreateInfo, + result = vk_device_init(&device->vk, NULL, NULL, pCreateInfo, &physical_device->instance->alloc, pAllocator); if (result != VK_SUCCESS) { vk_free(&device->vk.alloc, device); diff --git a/src/freedreno/vulkan/tu_device.c b/src/freedreno/vulkan/tu_device.c index 1fdd96d..e7a21a8 100644 --- a/src/freedreno/vulkan/tu_device.c +++ b/src/freedreno/vulkan/tu_device.c @@ -1041,7 +1041,7 @@ tu_CreateDevice(VkPhysicalDevice physicalDevice, if (!device) return vk_startup_errorf(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY, "OOM"); - result = vk_device_init(&device->vk, pCreateInfo, + result = vk_device_init(&device->vk, NULL, NULL, pCreateInfo, &physical_device->instance->alloc, pAllocator); if (result != VK_SUCCESS) { vk_free(&device->vk.alloc, device); diff --git a/src/gallium/frontends/lavapipe/lvp_device.c b/src/gallium/frontends/lavapipe/lvp_device.c index 4512a0d..3a3a963 100644 --- a/src/gallium/frontends/lavapipe/lvp_device.c +++ b/src/gallium/frontends/lavapipe/lvp_device.c @@ -937,7 +937,7 @@ VkResult lvp_CreateDevice( if (!device) return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY); - VkResult result = vk_device_init(&device->vk, pCreateInfo, + VkResult result = vk_device_init(&device->vk, NULL, NULL, pCreateInfo, &physical_device->instance->alloc, pAllocator); if (result != VK_SUCCESS) { diff --git a/src/intel/Android.vulkan.mk b/src/intel/Android.vulkan.mk index 10e72c1..1d4f389 100644 --- a/src/intel/Android.vulkan.mk +++ b/src/intel/Android.vulkan.mk @@ -53,6 +53,7 @@ VULKAN_COMMON_HEADER_LIBRARIES := \ endif ANV_STATIC_LIBRARIES := \ + libmesa_vulkan_util \ libmesa_vulkan_common \ libmesa_genxml \ libmesa_nir diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c index 8931c6b..37e5597 100644 --- a/src/intel/vulkan/anv_device.c +++ b/src/intel/vulkan/anv_device.c @@ -2860,7 +2860,7 @@ VkResult anv_CreateDevice( if (!device) return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY); - result = vk_device_init(&device->vk, pCreateInfo, + result = vk_device_init(&device->vk, NULL, NULL, pCreateInfo, &physical_device->instance->alloc, pAllocator); if (result != VK_SUCCESS) { vk_error(result); diff --git a/src/vulkan/Makefile.sources b/src/vulkan/Makefile.sources index 1bfbca6..d5cbf01 100644 --- a/src/vulkan/Makefile.sources +++ b/src/vulkan/Makefile.sources @@ -32,8 +32,12 @@ VULKAN_UTIL_FILES := \ util/vk_device.c \ util/vk_device.h \ util/vk_format.c \ + util/vk_instance.c \ + util/vk_instance.h \ util/vk_object.c \ util/vk_object.h \ + util/vk_physical_device.c \ + util/vk_physical_device.h \ util/vk_util.c \ util/vk_util.h diff --git a/src/vulkan/util/meson.build b/src/vulkan/util/meson.build index c8f9cec..4db5b75 100644 --- a/src/vulkan/util/meson.build +++ b/src/vulkan/util/meson.build @@ -29,8 +29,12 @@ files_vulkan_util = files( 'vk_device.c', 'vk_device.h', 'vk_format.c', + 'vk_instance.c', + 'vk_instance.h', 'vk_object.c', 'vk_object.h', + 'vk_physical_device.c', + 'vk_physical_device.h', 'vk_util.c', 'vk_util.h', ) diff --git a/src/vulkan/util/vk_device.c b/src/vulkan/util/vk_device.c index 457169a..10d55fd 100644 --- a/src/vulkan/util/vk_device.c +++ b/src/vulkan/util/vk_device.c @@ -23,21 +23,49 @@ #include "vk_device.h" +#include "vk_physical_device.h" #include "util/hash_table.h" #include "util/ralloc.h" VkResult vk_device_init(struct vk_device *device, - UNUSED const VkDeviceCreateInfo *pCreateInfo, + struct vk_physical_device *physical_device, + const struct vk_device_dispatch_table *dispatch_table, + const VkDeviceCreateInfo *pCreateInfo, const VkAllocationCallbacks *instance_alloc, const VkAllocationCallbacks *device_alloc) { + memset(device, 0, sizeof(*device)); vk_object_base_init(device, &device->base, VK_OBJECT_TYPE_DEVICE); if (device_alloc) device->alloc = *device_alloc; else device->alloc = *instance_alloc; + device->physical = physical_device; + + if (dispatch_table != NULL) + device->dispatch_table = *dispatch_table; + + if (physical_device != NULL) { + for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { + int idx; + for (idx = 0; idx < VK_DEVICE_EXTENSION_COUNT; idx++) { + if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], + vk_device_extensions[idx].extensionName) == 0) + break; + } + + if (idx >= VK_DEVICE_EXTENSION_COUNT) + return VK_ERROR_EXTENSION_NOT_PRESENT; + + if (!physical_device->supported_extensions.extensions[idx]) + return VK_ERROR_EXTENSION_NOT_PRESENT; + + device->enabled_extensions.extensions[idx] = true; + } + } + p_atomic_set(&device->private_data_next_index, 0); #ifdef ANDROID diff --git a/src/vulkan/util/vk_device.h b/src/vulkan/util/vk_device.h index 1a86e58..9fb1c4e 100644 --- a/src/vulkan/util/vk_device.h +++ b/src/vulkan/util/vk_device.h @@ -23,6 +23,8 @@ #ifndef VK_DEVICE_H #define VK_DEVICE_H +#include "vk_dispatch_table.h" +#include "vk_extensions.h" #include "vk_object.h" #ifdef __cplusplus @@ -32,6 +34,11 @@ extern "C" { struct vk_device { struct vk_object_base base; VkAllocationCallbacks alloc; + struct vk_physical_device *physical; + + struct vk_device_extension_table enabled_extensions; + + struct vk_device_dispatch_table dispatch_table; /* For VK_EXT_private_data */ uint32_t private_data_next_index; @@ -44,6 +51,8 @@ struct vk_device { VkResult MUST_CHECK vk_device_init(struct vk_device *device, + struct vk_physical_device *physical_device, + const struct vk_device_dispatch_table *dispatch_table, const VkDeviceCreateInfo *pCreateInfo, const VkAllocationCallbacks *instance_alloc, const VkAllocationCallbacks *device_alloc); diff --git a/src/vulkan/util/vk_instance.c b/src/vulkan/util/vk_instance.c new file mode 100644 index 0000000..6ede08a --- /dev/null +++ b/src/vulkan/util/vk_instance.c @@ -0,0 +1,90 @@ +/* + * Copyright © 2021 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "vk_instance.h" + +#include "vk_alloc.h" + +VkResult +vk_instance_init(struct vk_instance *instance, + const struct vk_instance_extension_table *supported_extensions, + const struct vk_instance_dispatch_table *dispatch_table, + const VkInstanceCreateInfo *pCreateInfo, + const VkAllocationCallbacks *alloc) +{ + memset(instance, 0, sizeof(*instance)); + vk_object_base_init(NULL, &instance->base, VK_OBJECT_TYPE_INSTANCE); + instance->alloc = *alloc; + + instance->app_info = (struct vk_app_info) { .api_version = 0 }; + if (pCreateInfo->pApplicationInfo) { + const VkApplicationInfo *app = pCreateInfo->pApplicationInfo; + + instance->app_info.app_name = + vk_strdup(&instance->alloc, app->pApplicationName, + VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); + instance->app_info.app_version = app->applicationVersion; + + instance->app_info.engine_name = + vk_strdup(&instance->alloc, app->pEngineName, + VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); + instance->app_info.engine_version = app->engineVersion; + + instance->app_info.api_version = app->apiVersion; + } + + if (instance->app_info.api_version == 0) + instance->app_info.api_version = VK_API_VERSION_1_0; + + if (supported_extensions != NULL) { + for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) { + int idx; + for (idx = 0; idx < VK_INSTANCE_EXTENSION_COUNT; idx++) { + if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], + vk_instance_extensions[idx].extensionName) == 0) + break; + } + + if (idx >= VK_INSTANCE_EXTENSION_COUNT) + return VK_ERROR_EXTENSION_NOT_PRESENT; + + if (!supported_extensions->extensions[idx]) + return VK_ERROR_EXTENSION_NOT_PRESENT; + + instance->enabled_extensions.extensions[idx] = true; + } + } + + if (dispatch_table != NULL) + instance->dispatch_table = *dispatch_table; + + return VK_SUCCESS; +} + +void +vk_instance_finish(struct vk_instance *instance) +{ + vk_free(&instance->alloc, (char *)instance->app_info.app_name); + vk_free(&instance->alloc, (char *)instance->app_info.engine_name); + vk_object_base_finish(&instance->base); +} diff --git a/src/vulkan/util/vk_instance.h b/src/vulkan/util/vk_instance.h new file mode 100644 index 0000000..8704a76 --- /dev/null +++ b/src/vulkan/util/vk_instance.h @@ -0,0 +1,66 @@ +/* + * Copyright © 2021 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef VK_INSTANCE_H +#define VK_INSTANCE_H + +#include "vk_dispatch_table.h" +#include "vk_extensions.h" +#include "vk_object.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct vk_app_info { + const char* app_name; + uint32_t app_version; + const char* engine_name; + uint32_t engine_version; + uint32_t api_version; +}; + +struct vk_instance { + struct vk_object_base base; + VkAllocationCallbacks alloc; + + struct vk_app_info app_info; + struct vk_instance_extension_table enabled_extensions; + + struct vk_instance_dispatch_table dispatch_table; +}; + +VkResult MUST_CHECK +vk_instance_init(struct vk_instance *instance, + const struct vk_instance_extension_table *supported_extensions, + const struct vk_instance_dispatch_table *dispatch_table, + const VkInstanceCreateInfo *pCreateInfo, + const VkAllocationCallbacks *alloc); + +void +vk_instance_finish(struct vk_instance *instance); + +#ifdef __cplusplus +} +#endif + +#endif /* VK_INSTANCE_H */ diff --git a/src/vulkan/util/vk_physical_device.c b/src/vulkan/util/vk_physical_device.c new file mode 100644 index 0000000..6a14e46 --- /dev/null +++ b/src/vulkan/util/vk_physical_device.c @@ -0,0 +1,49 @@ +/* + * Copyright © 2021 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "vk_physical_device.h" + +VkResult +vk_physical_device_init(struct vk_physical_device *pdevice, + struct vk_instance *instance, + const struct vk_device_extension_table *supported_extensions, + const struct vk_physical_device_dispatch_table *dispatch_table) +{ + memset(pdevice, 0, sizeof(*pdevice)); + vk_object_base_init(NULL, &pdevice->base, VK_OBJECT_TYPE_PHYSICAL_DEVICE); + pdevice->instance = instance; + + if (supported_extensions != NULL) + pdevice->supported_extensions = *supported_extensions; + + if (dispatch_table != NULL) + pdevice->dispatch_table = *dispatch_table; + + return VK_SUCCESS; +} + +void +vk_physical_device_finish(struct vk_physical_device *physical_device) +{ + vk_object_base_finish(&physical_device->base); +} diff --git a/src/vulkan/util/vk_physical_device.h b/src/vulkan/util/vk_physical_device.h new file mode 100644 index 0000000..a490a66 --- /dev/null +++ b/src/vulkan/util/vk_physical_device.h @@ -0,0 +1,56 @@ +/* + * Copyright © 2021 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef VK_PHYSICAL_DEVICE_H +#define VK_PHYSICAL_DEVICE_H + +#include "vk_dispatch_table.h" +#include "vk_extensions.h" +#include "vk_object.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct vk_physical_device { + struct vk_object_base base; + struct vk_instance *instance; + + struct vk_device_extension_table supported_extensions; + + struct vk_physical_device_dispatch_table dispatch_table; +}; + +VkResult MUST_CHECK +vk_physical_device_init(struct vk_physical_device *physical_device, + struct vk_instance *instance, + const struct vk_device_extension_table *supported_extensions, + const struct vk_physical_device_dispatch_table *dispatch_table); + +void +vk_physical_device_finish(struct vk_physical_device *physical_device); + +#ifdef __cplusplus +} +#endif + +#endif /* VK_PHYSICAL_DEVICE_H */ -- 2.7.4