gpt4 book ai didi

c++ - Vulkan API 调试函数指针类型转换错误

转载 作者:行者123 更新时间:2023-11-30 03:33:26 24 4
gpt4 key购买 nike

这几天一直被这个问题困扰。我正在尝试使用 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 错误

Severity Code Description Project File Line Source Suppression State Error C2440 '=': cannot convert from 'VkBool32 (__cdecl *)(VkDebugReportFlagsEXT,VkDebugReportObjectTypeEXT,uint64_t,std::size_t,uint32_t,const char *,const char *,void *)' to 'PFN_vkDebugReportCallbackEXT' 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);
}
}

最佳答案

你为什么要转换函数??你不应该!该函数必须是所需的类型。

我还在 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" );

所以扫描你的代码我发现了这些问题:

  1. 您正在转换函数类型。
  2. PFN_vkDebugReportCallbackEXT::msgCodeint32_t 而不是 uint32_t
  3. 除非您知道自己在做什么,否则您可能不应该从回调中返回 VK_TRUE
  4. 为什么有*instance还有VkInstance&??没有意义。它是一个 Handlebars 。直接使用VkInstance;没有指针,没有取消引用,也不需要引用。
  5. dbgReportCreateInfo.flags 中,您真的想要 VK_DEBUG_REPORT_DEBUG_BIT_EXT 吗?考虑到您不接受 INFO,这个选择有点奇怪。

1 和 2 应该可以解决您眼前的问题。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com