vulkan: Add common instance and physical device structs

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8676>
This commit is contained in:
Jason Ekstrand 2021-01-23 13:06:02 -06:00 committed by Marge Bot
parent 0f22fdad95
commit d360a996f9
15 changed files with 314 additions and 7 deletions

View file

@ -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,

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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) {

View file

@ -53,6 +53,7 @@ VULKAN_COMMON_HEADER_LIBRARIES := \
endif
ANV_STATIC_LIBRARIES := \
libmesa_vulkan_util \
libmesa_vulkan_common \
libmesa_genxml \
libmesa_nir

View file

@ -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);

View file

@ -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

View file

@ -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',
)

View file

@ -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

View file

@ -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);

View file

@ -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);
}

View file

@ -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 */

View file

@ -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);
}

View file

@ -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 */