我正在使用Vulkan和SDL2启动游戏引擎。我的程序在代码的非常特定的部分崩溃。我真的是Vulkan API的新手,我尝试了几次修改都没有成功。如果有人可以帮助我,请在此处提供完整的代码以及我认为它会崩溃的部分。
// GE_Base.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vulkan/vulkan.h>
#include <SDL.h>
#include <glm.hpp>
#include <SDL_vulkan.h>
#include "GE_code_class.h"
#define WIDTH 1280
#define HEIGHT 720
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Window * window = SDL_CreateWindow("GE_Base", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH, HEIGHT, SDL_WINDOW_VULKAN);
////////////////////////////////
//VULKAN SETUP
const char* instanceExtensionNames[] = {
VK_KHR_SURFACE_EXTENSION_NAME,
};
VkApplicationInfo ai = {};
ai.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
ai.pNext = nullptr;
ai.pApplicationName = "SDL Vulkan";
ai.applicationVersion = 1;
ai.pEngineName = "SDLV";
ai.engineVersion = 1;
ai.apiVersion = VK_MAKE_VERSION(1, 0, 3);
VkInstanceCreateInfo ici = {};
ici.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
ici.pNext = nullptr;
ici.flags = 0;
ici.pApplicationInfo = &ai;
ici.enabledLayerCount = 0;
ici.ppEnabledLayerNames = nullptr;
ici.enabledExtensionCount = 0;
ici.ppEnabledExtensionNames = nullptr;
VkInstance instance;
vkCreateInstance(&ici, nullptr, &instance);
//CHECK PHYSICAL DEVICE (GRAFIC CARD)
VK_DEFINE_HANDLE(VkPhysicalDevice);
uint32_t deviceCount = 1;
VkPhysicalDevice gpu;
//VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)malloc(sizeof(deviceCount));
VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)malloc(sizeof(deviceCount));
vkEnumeratePhysicalDevices(instance, &deviceCount, physical_devices);
gpu = physical_devices[0];
//CHECK QUEUE FAMILIES
uint32_t graphics_queue_node_index;
/*if (queue_props->queueCount <= 0) queue_props->queueCount = 1;*/
uint32_t amountOfFamilies = 0;
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &amountOfFamilies, NULL);
VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[amountOfFamilies];
vkGetPhysicalDeviceQueueFamilyProperties(gpu, &amountOfFamilies, queue_props);
assert(queue_props->queueCount >= 1);
VkPhysicalDeviceFeatures features;
vkGetPhysicalDeviceFeatures(gpu, &features);
uint32_t graphicsQueueNodeIndex = UINT32_MAX;
unsigned int i;
for (i = 0; i < queue_props->queueCount; i++) {
if ((queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
if (graphicsQueueNodeIndex == UINT32_MAX)
graphicsQueueNodeIndex = i;
}
}
graphics_queue_node_index = graphicsQueueNodeIndex;
//LOGICAL DEVICE
VkDevice device;
float queue_priorities[1] = { 0.0 };
VkDeviceQueueCreateInfo q;
q.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
q.pNext = nullptr,
q.queueFamilyIndex = graphics_queue_node_index,
q.queueCount = 1,
q.pQueuePriorities = queue_priorities;
graphicsQueueNodeIndex = 2000;
graphics_queue_node_index = graphicsQueueNodeIndex;
//CREATE & INSTANTIATE DEVICE
VkDeviceCreateInfo d;
d.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
d.pNext = nullptr,
d.queueCreateInfoCount = 1,
d.pQueueCreateInfos = &q,
d.enabledLayerCount = 1,
d.ppEnabledLayerNames = nullptr,
d.enabledExtensionCount = 1,
d.ppEnabledExtensionNames = (const char *const *) instanceExtensionNames,
d.pEnabledFeatures = nullptr;
vkCreateDevice(gpu, &d, nullptr, &device);
//SURFACE WINDOW
VkSurfaceKHR surface;
SDL_Vulkan_CreateSurface(window, (SDL_vulkanInstance)&ici, (SDL_vulkanSurface*)&surface);
//PRESENTATION QUEU
VkBool32 *supportsPresent = new VkBool32[queue_props->queueCount];
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, queue_props->queueCount, surface, &supportsPresent[i]);
graphics_queue_node_index = graphicsQueueNodeIndex;
//QUEUE
VK_DEFINE_HANDLE(VkQueue)
VkQueue queu_;
vkGetDeviceQueue(device, graphics_queue_node_index, queue_props->queueCount, &queu_);
////////////////////////////////
GE_class *engine;
engine = new GE_class();
engine->Init(1280, 720);
bool quit_app = false;
while (!quit_app) {
SDL_Event eve;
while (SDL_PollEvent(&eve)) {
switch (eve.type) {
case SDL_WINDOWEVENT:
if (eve.window.event == SDL_WINDOWEVENT_RESIZED) {
}
break;
case SDL_QUIT:
quit_app = true;
break;
}
}
//double currentTime = (double)SDL_GetTicks() / 1000.0;
//GLrender(currentTime);
//double currentTime = (double) SDL_GetTicks() / 1000.0;
//myRenderCode(currentTime);
//SDL_GL_SwapWindow(window);
/*vkQueuePresentKHR()*/
}
//SDL_DestroyWindow(window);
//SDL_Quit();
return 0;
}
VkQueue queu_; vkGetDeviceQueue(device, graphics_queue_node_index, queue_props-queueCount, &queu_);
这些是我认为崩溃的行。我猜在前几行中 出现错误或我没有初始化某些变量。我不太确定。
答案 0 :(得分:1)
调用graphics_queue_node_index
时vkGetDeviceQueue
的值为2000。这很可能不是有效的队列索引。
VkDeviceQueueCreateInfo q;
// ...
q.queueFamilyIndex = graphics_queue_node_index,
// ...
graphicsQueueNodeIndex = 2000;
graphics_queue_node_index = graphicsQueueNodeIndex;
// ...
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, queue_props->queueCount, surface, &supportsPresent[i]);
graphics_queue_node_index = graphicsQueueNodeIndex; // redundant?
// ...
vkGetDeviceQueue(device, graphics_queue_node_index, queue_props->queueCount, &queu_);