Path: blob/21.2-virgl/src/virtio/vulkan/vn_common.h
4560 views
/*1* Copyright 2019 Google LLC2* SPDX-License-Identifier: MIT3*4* based in part on anv and radv which are:5* Copyright © 2015 Intel Corporation6* Copyright © 2016 Red Hat.7* Copyright © 2016 Bas Nieuwenhuizen8*/910#ifndef VN_COMMON_H11#define VN_COMMON_H1213#include <assert.h>14#include <inttypes.h>15#include <limits.h>16#include <stdatomic.h>17#include <stdbool.h>18#include <stddef.h>19#include <stdint.h>20#include <stdlib.h>21#include <string.h>22#include <vulkan/vulkan.h>2324#include "c11/threads.h"25#include "util/bitscan.h"26#include "util/compiler.h"27#include "util/list.h"28#include "util/macros.h"29#include "util/os_time.h"30#include "util/u_math.h"31#include "util/xmlconfig.h"32#include "vk_alloc.h"33#include "vk_debug_report.h"34#include "vk_device.h"35#include "vk_instance.h"36#include "vk_object.h"37#include "vk_physical_device.h"38#include "vk_util.h"3940#include "vn_entrypoints.h"4142#define VN_DEFAULT_ALIGN 84344#define VN_DEBUG(category) (unlikely(vn_debug & VN_DEBUG_##category))4546#define vn_error(instance, error) \47(VN_DEBUG(RESULT) ? vn_log_result((instance), (error), __func__) : (error))48#define vn_result(instance, result) \49((result) >= VK_SUCCESS ? (result) : vn_error((instance), (result)))5051struct vn_instance;52struct vn_physical_device;53struct vn_device;54struct vn_queue;55struct vn_fence;56struct vn_semaphore;57struct vn_device_memory;58struct vn_buffer;59struct vn_buffer_view;60struct vn_image;61struct vn_image_view;62struct vn_sampler;63struct vn_sampler_ycbcr_conversion;64struct vn_descriptor_set_layout;65struct vn_descriptor_pool;66struct vn_descriptor_set;67struct vn_descriptor_update_template;68struct vn_render_pass;69struct vn_framebuffer;70struct vn_event;71struct vn_query_pool;72struct vn_shader_module;73struct vn_pipeline_layout;74struct vn_pipeline_cache;75struct vn_pipeline;76struct vn_command_pool;77struct vn_command_buffer;7879struct vn_cs_encoder;80struct vn_cs_decoder;8182struct vn_renderer;83struct vn_renderer_shmem;84struct vn_renderer_bo;85struct vn_renderer_sync;8687enum vn_debug {88VN_DEBUG_INIT = 1ull << 0,89VN_DEBUG_RESULT = 1ull << 1,90VN_DEBUG_VTEST = 1ull << 2,91VN_DEBUG_WSI = 1ull << 3,92};9394typedef uint64_t vn_object_id;9596/* base class of vn_instance */97struct vn_instance_base {98struct vk_instance base;99vn_object_id id;100};101102/* base class of vn_physical_device */103struct vn_physical_device_base {104struct vk_physical_device base;105vn_object_id id;106};107108/* base class of vn_device */109struct vn_device_base {110struct vk_device base;111vn_object_id id;112};113114/* base class of other driver objects */115struct vn_object_base {116struct vk_object_base base;117vn_object_id id;118};119120extern uint64_t vn_debug;121122void123vn_debug_init(void);124125void126vn_log(struct vn_instance *instance, const char *format, ...)127PRINTFLIKE(2, 3);128129VkResult130vn_log_result(struct vn_instance *instance,131VkResult result,132const char *where);133134void135vn_relax(uint32_t *iter);136137static_assert(sizeof(vn_object_id) >= sizeof(uintptr_t), "");138139static inline VkResult140vn_instance_base_init(141struct vn_instance_base *instance,142const struct vk_instance_extension_table *supported_extensions,143const struct vk_instance_dispatch_table *dispatch_table,144const VkInstanceCreateInfo *info,145const VkAllocationCallbacks *alloc)146{147VkResult result = vk_instance_init(&instance->base, supported_extensions,148dispatch_table, info, alloc);149instance->id = (uintptr_t)instance;150return result;151}152153static inline void154vn_instance_base_fini(struct vn_instance_base *instance)155{156vk_instance_finish(&instance->base);157}158159static inline VkResult160vn_physical_device_base_init(161struct vn_physical_device_base *physical_dev,162struct vn_instance_base *instance,163const struct vk_device_extension_table *supported_extensions,164const struct vk_physical_device_dispatch_table *dispatch_table)165{166VkResult result =167vk_physical_device_init(&physical_dev->base, &instance->base,168supported_extensions, dispatch_table);169physical_dev->id = (uintptr_t)physical_dev;170return result;171}172173static inline void174vn_physical_device_base_fini(struct vn_physical_device_base *physical_dev)175{176vk_physical_device_finish(&physical_dev->base);177}178179static inline VkResult180vn_device_base_init(struct vn_device_base *dev,181struct vn_physical_device_base *physical_dev,182const struct vk_device_dispatch_table *dispatch_table,183const VkDeviceCreateInfo *info,184const VkAllocationCallbacks *alloc)185{186VkResult result = vk_device_init(&dev->base, &physical_dev->base,187dispatch_table, info, alloc);188dev->id = (uintptr_t)dev;189return result;190}191192static inline void193vn_device_base_fini(struct vn_device_base *dev)194{195vk_device_finish(&dev->base);196}197198static inline void199vn_object_base_init(struct vn_object_base *obj,200VkObjectType type,201struct vn_device_base *dev)202{203vk_object_base_init(&dev->base, &obj->base, type);204obj->id = (uintptr_t)obj;205}206207static inline void208vn_object_base_fini(struct vn_object_base *obj)209{210vk_object_base_finish(&obj->base);211}212213static inline void214vn_object_set_id(void *obj, vn_object_id id, VkObjectType type)215{216assert(((const struct vk_object_base *)obj)->type == type);217switch (type) {218case VK_OBJECT_TYPE_INSTANCE:219((struct vn_instance_base *)obj)->id = id;220break;221case VK_OBJECT_TYPE_PHYSICAL_DEVICE:222((struct vn_physical_device_base *)obj)->id = id;223break;224case VK_OBJECT_TYPE_DEVICE:225((struct vn_device_base *)obj)->id = id;226break;227default:228((struct vn_object_base *)obj)->id = id;229break;230}231}232233static inline vn_object_id234vn_object_get_id(const void *obj, VkObjectType type)235{236assert(((const struct vk_object_base *)obj)->type == type);237switch (type) {238case VK_OBJECT_TYPE_INSTANCE:239return ((struct vn_instance_base *)obj)->id;240case VK_OBJECT_TYPE_PHYSICAL_DEVICE:241return ((struct vn_physical_device_base *)obj)->id;242case VK_OBJECT_TYPE_DEVICE:243return ((struct vn_device_base *)obj)->id;244default:245return ((struct vn_object_base *)obj)->id;246}247}248249#endif /* VN_COMMON_H */250251252