简体   繁体   English

Vulkan Vulkan.hpp从对象实例获取opbject类型

[英]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? 有没有办法从实例获取对象类型枚举器( vk::ObjectType dor vulkan.hpp和VkObjectType的VkObjectType)?

Eg assume we have 3 objects: 例如,假设我们有3个对象:

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

and a function f() such that: 和函数f()使得:

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. 这是否在lunar sdk,第三方库还是仅是一个聪明的hack中都没有关系。

If you could do that, then VkDebugUtilsObjectNameInfoEXT wouldn't need to take an objectType , would it ;) 如果可以做到这一点,那么VkDebugUtilsObjectNameInfoEXT就不需要采用objectType了;)

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. 因此,您有两种选择, VkObjectType选择都需要相同的东西:从C / C ++对象类型到实际VkObjectType枚举器的映射表。

There's the runtime choice, where you build a map of some kind which maps from a std::type_index to the VkObjectType . 有运行时选择,您可以在其中构建某种类型的映射,该映射从std::type_indexVkObjectType With std::map , you'd have this: 使用std::map ,您将拥有:

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. 您可以使用宏来使表定义更好看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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