安装完 Vulkan 目录后,有 Visual Studio 的模板,将模板拷贝到 Visual Studio 的模板目录下(C:\Users\username\Documents\Visual Studio YYYY\Templates\ProjectTemplates\Visual C++ Project)。
private: // Use validation layers if this is a debug build std::vector<constchar*> layers; // Create the Vulkan instance. vk::Instance instance; // Create a Vulkan surface for rendering VkSurfaceKHR c_surface; vk::SurfaceKHR surface{ c_surface }; };
boolGame::Initialize() { // Create an SDL window that supports Vulkan rendering. if (SDL_Init(SDL_INIT_VIDEO) != 0) { SDL_Log("Could not initialize SDL."); returnfalse; } mWindow = SDL_CreateWindow("Vulkan Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_VULKAN); if (mWindow == nullptr) { SDL_Log("Could not create SDL window."); returnfalse; }
// Get WSI extensions from SDL (we can add more if we like - we just can't remove these) unsigned extension_count; if (!SDL_Vulkan_GetInstanceExtensions(mWindow, &extension_count, nullptr)) { SDL_Log("Could not get the number of required instance extensions from SDL."); returnfalse; }
std::vector<constchar*> extensions(extension_count); if (!SDL_Vulkan_GetInstanceExtensions(mWindow, &extension_count, extensions.data())) { SDL_Log("Could not get the names of required instance extensions from SDL."); returnfalse; }
// vk::ApplicationInfo allows the programmer to specifiy some basic information about the // program, which can be useful for layers and tools to provide more debug information. vk::ApplicationInfo appInfo = vk::ApplicationInfo() .setPApplicationName("Vulkan C++ Windowed Program Template") .setApplicationVersion(1) .setPEngineName("LunarG SDK") .setEngineVersion(1) .setApiVersion(VK_API_VERSION_1_0);
// vk::InstanceCreateInfo is where the programmer specifies the layers and/or extensions that // are needed. vk::InstanceCreateInfo instInfo = vk::InstanceCreateInfo() .setFlags(vk::InstanceCreateFlags()) .setPApplicationInfo(&appInfo) .setEnabledExtensionCount(static_cast<uint32_t>(extensions.size())) .setPpEnabledExtensionNames(extensions.data()) .setEnabledLayerCount(static_cast<uint32_t>(layers.size())) .setPpEnabledLayerNames(layers.data());