Path: blob/21.2-virgl/src/vulkan/util/vk_object.h
7130 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*/22#ifndef VK_OBJECT_H23#define VK_OBJECT_H2425#include <vulkan/vulkan.h>26#include <vulkan/vk_icd.h>2728#include "c11/threads.h"29#include "util/macros.h"30#include "util/sparse_array.h"3132#ifdef __cplusplus33extern "C" {34#endif3536struct hash_table;3738struct vk_device;3940struct vk_object_base {41VK_LOADER_DATA _loader_data;42VkObjectType type;4344struct vk_device *device;4546/* For VK_EXT_private_data */47struct util_sparse_array private_data;48};4950void vk_object_base_init(UNUSED struct vk_device *device,51struct vk_object_base *base,52UNUSED VkObjectType obj_type);53void vk_object_base_finish(UNUSED struct vk_object_base *base);54void vk_object_base_reset(struct vk_object_base *base);5556static inline void57vk_object_base_assert_valid(ASSERTED struct vk_object_base *base,58ASSERTED VkObjectType obj_type)59{60assert(base == NULL || base->type == obj_type);61}6263static inline struct vk_object_base *64vk_object_base_from_u64_handle(uint64_t handle, VkObjectType obj_type)65{66struct vk_object_base *base = (struct vk_object_base *)(uintptr_t)handle;67vk_object_base_assert_valid(base, obj_type);68return base;69}7071#define VK_DEFINE_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \72static inline struct __driver_type * \73__driver_type ## _from_handle(__VkType _handle) \74{ \75struct vk_object_base *base = (struct vk_object_base *)_handle; \76vk_object_base_assert_valid(base, __VK_TYPE); \77STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \78return (struct __driver_type *) base; \79} \80\81static inline __VkType \82__driver_type ## _to_handle(struct __driver_type *_obj) \83{ \84vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \85return (__VkType) _obj; \86}8788#define VK_DEFINE_NONDISP_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \89static inline struct __driver_type * \90__driver_type ## _from_handle(__VkType _handle) \91{ \92struct vk_object_base *base = \93(struct vk_object_base *)(uintptr_t)_handle; \94vk_object_base_assert_valid(base, __VK_TYPE); \95STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \96return (struct __driver_type *)base; \97} \98\99static inline __VkType \100__driver_type ## _to_handle(struct __driver_type *_obj) \101{ \102vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \103return (__VkType)(uintptr_t) _obj; \104}105106#define VK_FROM_HANDLE(__driver_type, __name, __handle) \107struct __driver_type *__name = __driver_type ## _from_handle(__handle)108109/* Helpers for vk object (de)allocation and (de)initialization */110void *111vk_object_alloc(struct vk_device *device,112const VkAllocationCallbacks *alloc,113size_t size,114VkObjectType vk_obj_type);115116void *117vk_object_zalloc(struct vk_device *device,118const VkAllocationCallbacks *alloc,119size_t size,120VkObjectType vk_obj_type);121122struct vk_multialloc;123124void *125vk_object_multialloc(struct vk_device *device,126struct vk_multialloc *ma,127const VkAllocationCallbacks *alloc,128VkObjectType vk_obj_type);129130void *131vk_object_multizalloc(struct vk_device *device,132struct vk_multialloc *ma,133const VkAllocationCallbacks *alloc,134VkObjectType vk_obj_type);135136void137vk_object_free(struct vk_device *device,138const VkAllocationCallbacks *alloc,139void *data);140141142struct vk_private_data_slot {143struct vk_object_base base;144uint32_t index;145};146VK_DEFINE_NONDISP_HANDLE_CASTS(vk_private_data_slot, base,147VkPrivateDataSlotEXT,148VK_OBJECT_TYPE_PRIVATE_DATA_SLOT_EXT);149150VkResult151vk_private_data_slot_create(struct vk_device *device,152const VkPrivateDataSlotCreateInfoEXT* pCreateInfo,153const VkAllocationCallbacks* pAllocator,154VkPrivateDataSlotEXT* pPrivateDataSlot);155void156vk_private_data_slot_destroy(struct vk_device *device,157VkPrivateDataSlotEXT privateDataSlot,158const VkAllocationCallbacks *pAllocator);159VkResult160vk_object_base_set_private_data(struct vk_device *device,161VkObjectType objectType,162uint64_t objectHandle,163VkPrivateDataSlotEXT privateDataSlot,164uint64_t data);165void166vk_object_base_get_private_data(struct vk_device *device,167VkObjectType objectType,168uint64_t objectHandle,169VkPrivateDataSlotEXT privateDataSlot,170uint64_t *pData);171172#ifdef __cplusplus173}174#endif175176#endif /* VK_OBJECT_H */177178179