简体   繁体   中英

Vulkan Vulkan.hpp get opbject type from object instance

Is there a way to get the object type enumerator ( vk::ObjectType dor vulkan.hpp and VkObjectType for vulkan) from an instance?

Eg assume we have 3 objects:

vk::Device d;
vk::Buffer b;
vk::Queue q;

and a function f() such that:

f(d) returns: eDevice
f(b) returns: eBuffer
f(q) returns: eQueue 

It doesn't matter whether this is in the lunar sdk, a third party library, or just a clever hack.

If you could do that, then VkDebugUtilsObjectNameInfoEXT wouldn't need to take an objectType , would it ;)

So you have two choices, both of them requiring the same thing: a mapping table from the C/C++ object type to the actual VkObjectType enumerator.

There's the runtime choice, where you build a map of some kind which maps from a std::type_index to the VkObjectType . With std::map , you'd have this:

std::map<std::type_index, VkObjectType> objectMap = {
    {std::type_index(typeid(VkInstance)), VK_OBJECT_TYPE_INSTANCE},
    {std::type_index(typeid(VkPhysicalDevice)), VK_OBJECT_TYPE_PHYSICAL_DEVICE},
    {std::type_index(typeid(VkDevice)), VK_OBJECT_TYPE_DEVICE},
    ...
};

template<typename T>
void SetName(VkDevice *device, T *obj, const char *name)
{
    VkDebugUtilsObjectNameInfoEXT nameInfo =
    {
         VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
         nullptr,
         objectMap[typeid(T)],
         (reinterpret_cast<std::uint64_t>(obj),
         name,
    };

    vkSetDebugUtilsObjectNameEXT(device, &nameInfo);
}

A more compile-time solution can be had by using template specialization:

template<typename T>
struct VulkanObjectMap;

template struct VulkanObjectMap<VkInstance> { static VkObjectType value = VK_OBJECT_TYPE_INSTANCE; };
template struct VulkanObjectMap<VkPhysicalDevice> { static VkObjectType value = VK_OBJECT_TYPE_PHYSICAL_DEVICE; };
template struct VulkanObjectMap<VkDevice> { static VkObjectType value = VK_OBJECT_TYPE_DEVICE; };
...

template<typename T>
void SetName(VkDevice *device, T *obj, const char *name)
{
    VkDebugUtilsObjectNameInfoEXT nameInfo =
    {
         VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
         nullptr,
         VulkanObjectMap<T>::value,
         reinterpret_cast<std::uint64_t>(obj),
         name,
    };

    vkSetDebugUtilsObjectNameEXT(device, &nameInfo);
}

You can use a macro to make defining the table nicer to look at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM