Vulkan API Debug函数指针类型错误

时间:2017-03-21 05:47:28

标签: c++ struct types casting vulkan

过去几天一直坚持这个问题。我尝试使用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头文件

这与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错误

  

严重级代码说明项目文件行源抑制状态   错误C2440&#39; =&#39;:无法转换为&#39; VkBool32(__ cdecl   *)(VkDebugReportFlagsEXT,VkDebugReportObjectTypeEXT,uint64_t中,标准::为size_t,uint32_t的,常量   char *,const char *,void *)&#39;至   &#39; PFN_vkDebugReportCallbackEXT&#39; vulkanproject C:\ Users \ Alec \ workspace \ cpp \ vulkanproject \ source \ VulkanLayerAndExtension.cpp 287 Build

我的代码

我的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);
    }
}

1 个答案:

答案 0 :(得分:2)

你为什么要施放这个功能?你不应该!该功能必须是必需的类型。

我也在my example project中以编程方式进行调试,并且它有效,所以我们在这里进行比较:

for($i=0; $i < $length; $i++){

    $result[get_attribute_name()] = [array];

}

所以扫描你的代码我看到了这些问题:

  1. 您正在投射功能类型。
  2. 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::msgCode而不是int32_t
  3. 除非你知道自己在做什么,否则你可能不会从回调中返回uint32_t
  4. 为什么有VK_TRUE*instance?没有意义。这是一个句柄。直接使用VkInstance&;没有指针,没有解除引用,也没有引用。
  5. VkInstance你真的想要dbgReportCreateInfo.flags吗?考虑到你还不接受VK_DEBUG_REPORT_DEBUG_BIT_EXT,这是一个奇怪的选择。
  6. 1和2应该可以解决您的问题。