在过去的几天中一直困扰于此问题。我正在尝试使用Vulkan API的调试层,已成功获取PFN_vkCreateDebugReportCallbackEXTPFN_vkDestroyDebugReportCallbackEXT的函数指针。

但是,当创建该结构以用作vkCreateDebugReportCallback的参数时,我在该结构的pfnCallback部分遇到了问题。

在尝试编译时,Visual Studio会给我这个错误,我尝试如下进行类型转换:

dbgReportCreateInfo.pfnCallback = (PFN_vkDebugReportCallbackEXT) debugFunction;


dbgReportCreateInfo.pfnCallback = reinterpret_cast<PFN_vkDebugReportCallbackEXT>(debugFunction);

当调用vkCreateDebugReportCallback函数时,这两者都会导致内存访问冲突。

什么应该工作

从教科书Learning Vulkan和提供的存储库中,结构的PFN_vkDebugReportCallbackEXT pfn部分的正确声明应如其存储库here中所示。
VKAPI_ATTR VkBool32 VKAPI_CALL debugFunction(
    VkFlags                    msgFlags,
    VkDebugReportObjectTypeEXT objType,
    uint64_t                   srcObject,
    size_t                     location,
    uint32_t                   msgCode,
    const char *               layerPrefix,
    const char *               msg,
    void *                     userData);

Vulkan header 文件

就像在Vulkan头文件vulkan.h中定义的一样
typdef uint32_t VkBool32;

typedef VkBool32 (VKAPI_PTR *PFN_vkDebugReportCallbackEXT)(
    VkDebugReportFlagsEXT                       flags,
    VkDebugReportObjectTypeEXT                  objectType,
    uint64_t                                    object,
    size_t                                      location,
    int32_t                                     messageCode,
    const char*                                 pLayerPrefix,
    const char*                                 pMessage,
    void*                                       pUserData);


typedef struct VkDebugReportCallbackCreateInfoEXT {
    VkStructureType                 sType;
    const void*                     pNext;
    VkDebugReportFlagsEXT           flags;
    PFN_vkDebugReportCallbackEXT    pfnCallback;
    void*                           pUserData;
} VkDebugReportCallbackCreateInfoEXT;

Visual Studio错误



我的密码

我的Github repository中有完整的项目

VulkanLayerAndExtension.h
class VulkanLayerAndExtension {
    // Some stuff before
    VkResult createDebugReportCallback();
    void destroyDebugReportCallback();
    static VKAPI_ATTR VkBool32 VKAPI_CALL debugFunction(VkDebugReportFlagsEXT msgFlags,
        VkDebugReportObjectTypeEXT objType,
        uint64_t srcObject,
        size_t location,
        uint32_t msgCode,
        const char * layerPrefix,
        const char * msg,
        void * userData);

private:
    PFN_vkCreateDebugReportCallbackEXT dbgCreateDebugReportCallback;
    PFN_vkDestroyDebugReportCallbackEXT dbgDestroyDebugReportCallback;
    VkDebugReportCallbackEXT debugReportCallback;
public:
    VkDebugReportCallbackCreateInfoEXT dbgReportCreateInfo = {};

VulkanLayerAndExtension.cpp
// stuff before

VKAPI_ATTR VkBool32 VKAPI_CALL
VulkanLayerAndExtension::debugFunction(VkDebugReportFlagsEXT msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location, uint32_t msgCode, const char * layerPrefix, const char * msg, void * userData)
{
    std::cout << "[VK_DEBUG_REPORT] ";
    if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
        std::cout << "ERROR";
    }
    else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
        std::cout << "WARNING";
    }
    else if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
        std::cout << "INFORMATION";
    }
    else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
        std::cout << "PERFORMANCE";
    }
    else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
        std::cout << "DEBUG";
    }
    else {
        return VK_FALSE;
    }
    std::cout << ": [" << layerPrefix << "] Code" << msgCode << ":" << msg << std::endl;
    return VK_TRUE;
}

VkResult VulkanLayerAndExtension::createDebugReportCallback()
{
    VkResult result;

    VulkanApplication * appObj = VulkanApplication::GetInstance();
    VkInstance* instance = &appObj->instanceObj.instance;

    // Get vkCreateDebugReportCallbackEXT API
    dbgCreateDebugReportCallback = (PFN_vkCreateDebugReportCallbackEXT) vkGetInstanceProcAddr(*instance, "vkCreateDebugReportCallbackEXT");
    if (!dbgCreateDebugReportCallback) {
        std::cout << "Error: GetInstanceProcAddr unable to locate vkCreateDebugReportCallbackEXT function.\n";
        return VK_ERROR_INITIALIZATION_FAILED;
    }
    std::cout << "GetInstanceProcAddr loaded dbgCreateDebugReportCallback function.\n";

    // Get vkDestroyDebugReportCallbackEXT API
    dbgDestroyDebugReportCallback = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(*instance, "vkDestroyDebugReportCallbackEXT");
    if (!dbgDestroyDebugReportCallback) {
        std::cout << "Error: GetInstanceProcAddr unable to locate vkDestroyDebugReportCallbackEXT function.\n";
        return VK_ERROR_INITIALIZATION_FAILED;
    }
    std::cout << "GetInstanceProcAddr loaded dbgDestroyDebugReportCallback function.\n";

    dbgReportCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
    // Type error occurs on this line below
    dbgReportCreateInfo.pfnCallback =   VulkanLayerAndExtension::debugFunction;
    dbgReportCreateInfo.pUserData =     NULL;
    dbgReportCreateInfo.pNext =         NULL;
    dbgReportCreateInfo.flags =         VK_DEBUG_REPORT_WARNING_BIT_EXT |
                                        VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
                                        VK_DEBUG_REPORT_ERROR_BIT_EXT |
                                        VK_DEBUG_REPORT_DEBUG_BIT_EXT;

    // Create the debug report callback object
    result = dbgCreateDebugReportCallback(*instance, &dbgReportCreateInfo, NULL, &debugReportCallback);
    if (result == VK_SUCCESS) {
        std::cout << "Debug report callback object created successfully.\n";
    }

    return result;
}

void VulkanLayerAndExtension::destroyDebugReportCallback()
{
    VulkanApplication * appObj = VulkanApplication::GetInstance();
    VkInstance & instance = appObj->instanceObj.instance;
    if (debugReportCallback) {
        dbgDestroyDebugReportCallback(instance, debugReportCallback, NULL);
    }
}

最佳答案

为什么要强制转换功能?你不应该!该功能已经是必需的类型。

我也可以在my example project中以编程方式进行调试,并且它可以正常工作,因此请在此处进行比较:

VKAPI_ATTR VkBool32 VKAPI_CALL genericDebugCallback(
    VkDebugReportFlagsEXT msgFlags,
    VkDebugReportObjectTypeEXT objType,
    uint64_t srcObject,
    size_t /*location*/,
    int32_t msgCode,
    const char* pLayerPrefix,
    const char* pMsg,
    void* /*pUserData*/
){
    // just print everything
    return VK_FALSE;
}

//// elsewhere
VkDebugReportCallbackCreateInfoEXT debugCreateInfo{
    VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
    nullptr,
    debugAmount,
    genericDebugCallback,
    nullptr
};

VkDebugReportCallbackEXT debug;
VkResult errorCode = vkCreateDebugReportCallbackEXT( instance, &debugCreateInfo, nullptr, &debug );
RESULT_HANDLER( errorCode, "vkCreateDebugReportCallbackEXT" );

因此,扫描您的代码,我会看到以下问题:
  • 您正在转换函数类型。
  • PFN_vkDebugReportCallbackEXT::msgCodeint32_t而不是uint32_t
  • 除非您知道自己在做什么,否则可能不应该从回调中返回VK_TRUE
  • 为什么会有*instanceVkInstance&?没有意义。这是一个句柄。直接使用VkInstance;没有指针,没有取消引用,也不需要引用。
  • 您真的要dbgReportCreateInfo.flags吗?考虑到您还不接受VK_DEBUG_REPORT_DEBUG_BIT_EXT,这是一个奇怪的选择。

  • 1和2应该可以解决您的直接问题。

    关于c++ - Vulkan API调试功能指针类型转换错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42919301/

    10-11 04:26