[英]Creating Vulkan instance causes access violation
I am trying to initialize the Vulkan API. 我正在尝试初始化Vulkan API。
The problem I am having is that I get an access violation error after I call vkCreateInstance
and I think the problem comes from the extension and layer lists. 我遇到的问题是调用vkCreateInstance
后收到访问冲突错误,我认为问题来自扩展名和图层列表。
I am using a char buff[20][256] to transfer them from strings to the structure for the API call, and the layer and extension names I see in the debugger(3 extensions and 15 layers) are all a lot shorter than 256 characters and are all null terminated. 我正在使用char buff [20] [256]将它们从字符串传输到API调用的结构,并且在调试器中看到的层和扩展名(3个扩展和15层)都比256短很多字符,并且均以null终止。
There is no buffer overflow with the extension or layer names, yet it crashes. 扩展名或图层名没有缓冲区溢出,但崩溃了。
The layer and extension lists of strings I received trough using vkEnumerateInstanceExtensionProperties
and vkEnumerateInstanceLayerProperties
beforehand and are all valid null-terminated strings like "VK_KHR_surface", etc. 我事先使用vkEnumerateInstanceExtensionProperties
和vkEnumerateInstanceLayerProperties
接收到的字符串的层和扩展列表都是有效的以空字符结尾的字符串,例如“ VK_KHR_surface”等。
Is it possible that even tho it says I support some extensions, that I don't really support them and the API is crashing when it's trying to initialize an extension I don't support? 即使它说我支持某些扩展,是否真的不支持它们,并且在尝试初始化我不支持的扩展时API崩溃,是否有可能?
void InitializeInstance(void** instance, const vector<string>& layers, const vector<string>& extensions)
{
VkApplicationInfo applicationInfo;
VkInstanceCreateInfo instanceInfo;
VkInstance* instanceOut = (VkInstance*)instance;
applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
applicationInfo.pNext = nullptr;
applicationInfo.pApplicationName = "MyApp";
applicationInfo.pEngineName = "MyEngine";
applicationInfo.engineVersion = 1;
applicationInfo.apiVersion = VK_API_VERSION_1_0;
instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceInfo.pNext = null;
instanceInfo.flags = 0;
instanceInfo.pApplicationInfo = &applicationInfo;
char buffLayer[20][256];
char buffExt[20][256];
if(!layers.empty())
{
instanceInfo.enabledLayerCount = layers.size();
for(int i = 0; i < layers.size(); i++)
{
strcpy(buffLayer[i], layers[i].c_str());
}
instanceInfo.ppEnabledLayerNames = (char**)buffLayer;
}
else
{
instanceInfo.enabledLayerCount = 0;
instanceInfo.ppEnabledLayerNames = nullptr;
}
if(!extensions.empty())
{
instanceInfo.enabledExtensionCount = extensions.size();
for(int i = 0; i < extensions.size(); i++)
{
strcpy(buffExt[i], extensions[i].c_str());
}
instanceInfo.ppEnabledExtensionNames = (char**)buffExt;
}
else
{
instanceInfo.enabledExtensionCount = 0;
instanceInfo.ppEnabledExtensionNames = nullptr;
}
vkCreateInstance(&instanceInfo, nullptr, instanceOut);
}
When I have only 0 extensions AND 0 layers, it creates successfully. 当我只有0个扩展名和0个图层时,它创建成功。 If any of them is not 0, it crashes. 如果其中任何一个不为0,则崩溃。
char buffLayer[20][256];
instanceInfo.ppEnabledLayerNames = (char**)buffLayer;
ppEnabledLayerNames is supposed to be an array of pointers to character arrays. ppEnabledLayerNames应该是一个指向字符数组的指针的数组。 But you're passing it a 2D array of characters, which is effectively just an array of 20*256 characters. 但是,您要向其传递2D字符数组,实际上是20 * 256个字符的数组。
If you're on a machine with 32-bit pointers, the driver is going to take the first four bytes in buffLayer and treat them as a pointer to a character array. 如果您使用32位指针的计算机,则驱动程序将获取buffLayer中的前四个字节,并将其视为指向字符数组的指针。 But you've just stored the first four characters of a layer name there, and 'VK_K' is probably not going to be a valid pointer value :). 但是,您刚刚在其中存储了图层名称的前四个字符,并且'VK_K'可能不是有效的指针值:)。 So the loader will crash when trying to dereference that invalid pointer. 因此,当尝试取消引用该无效指针时,加载程序将崩溃。
Probably the simplest change would be to add: 可能最简单的更改是添加:
char* layerNames[20];
for (int i = 0; i < 20; i++)
layerNames[i] = &buffLayer[i][0];
and pass layerNames as ppEnabledLayerNames. 并将layerNames作为ppEnabledLayerNames传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.