Vulkan API 调试函数指针类型转换错误
Vulkan API Debug function pointer typecast Error
这几天一直被这个问题困扰。我正在尝试使用 Vulkan API 的调试层 我已经成功获取了 PFN_vkCreateDebugReportCallbackEXT
和 PFN_vkDestroyDebugReportCallbackEXT
.
的函数指针
但是在创建用作 vkCreateDebugReportCallback
参数的结构时,我遇到了结构的 pfnCallback
部分的问题。
Visual studio 在尝试编译时给我这个错误,我尝试按如下方式对其进行类型转换:
dbgReportCreateInfo.pfnCallback = (PFN_vkDebugReportCallbackEXT) debugFunction;
和
dbgReportCreateInfo.pfnCallback = reinterpret_cast<PFN_vkDebugReportCallbackEXT>(debugFunction);
当调用 vkCreateDebugReportCallback
函数时,两者都会导致内存访问冲突。
什么应该有效
来自课本Learning Vulkan and the supplied repository the proper declaration for the PFN_vkDebugReportCallbackEXT pfn
part of the struct should be as shown in their repository 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
中提供了完整的项目
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 = {};
// 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::msgCode
是 int32_t
不是 uint32_t
- 你可能不应该 return
VK_TRUE
来自回调,除非你知道你在做什么。
- 为什么有
*instance
还有VkInstance&
??没有意义。它是一个把手。直接使用VkInstance
;没有指针,没有取消引用,也不需要引用。
- 在
dbgReportCreateInfo.flags
你真的想要VK_DEBUG_REPORT_DEBUG_BIT_EXT
吗?考虑到您不接受 INFO
. ,这个选择有点奇怪
1 和 2 应该可以解决您眼前的问题。
这几天一直被这个问题困扰。我正在尝试使用 Vulkan API 的调试层 我已经成功获取了 PFN_vkCreateDebugReportCallbackEXT
和 PFN_vkDestroyDebugReportCallbackEXT
.
但是在创建用作 vkCreateDebugReportCallback
参数的结构时,我遇到了结构的 pfnCallback
部分的问题。
Visual studio 在尝试编译时给我这个错误,我尝试按如下方式对其进行类型转换:
dbgReportCreateInfo.pfnCallback = (PFN_vkDebugReportCallbackEXT) debugFunction;
和
dbgReportCreateInfo.pfnCallback = reinterpret_cast<PFN_vkDebugReportCallbackEXT>(debugFunction);
当调用 vkCreateDebugReportCallback
函数时,两者都会导致内存访问冲突。
什么应该有效
来自课本Learning Vulkan and the supplied repository the proper declaration for the PFN_vkDebugReportCallbackEXT pfn
part of the struct should be as shown in their repository 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
中提供了完整的项目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 = {};
// 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::msgCode
是int32_t
不是uint32_t
- 你可能不应该 return
VK_TRUE
来自回调,除非你知道你在做什么。 - 为什么有
*instance
还有VkInstance&
??没有意义。它是一个把手。直接使用VkInstance
;没有指针,没有取消引用,也不需要引用。 - 在
dbgReportCreateInfo.flags
你真的想要VK_DEBUG_REPORT_DEBUG_BIT_EXT
吗?考虑到您不接受INFO
. ,这个选择有点奇怪
1 和 2 应该可以解决您眼前的问题。