Skip to content

Instantly share code, notes, and snippets.

@MaverickTse
Created May 15, 2016 12:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaverickTse/c8061794d8f1dbfe08099b9329cb6605 to your computer and use it in GitHub Desktop.
Save MaverickTse/c8061794d8f1dbfe08099b9329cb6605 to your computer and use it in GitHub Desktop.
Vulkan initialization with GLFW and VK_CPP
// vulkan_fw_test.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"vulkan-1.lib") //DLL version
#pragma comment(lib,"glfw3.lib")
#define GLFW_INCLUDE_VULKAN //as required by GLFW
#include "stdafx.h" // optional headers
#include "vulkan\vk_cpp.hpp" //copy this header to your VulkanSDK include folder
#include "GLFW\glfw3.h" //GLFW header
#include <iostream>
using namespace std; //optionally using namespace vk;
int main()
{
if (!glfwInit()) //Initialize GLFW
{
cout << "GLFW not initialized." << endl;
abort();
}
if (!glfwVulkanSupported()) //Any Vulkan-related function requires GLFW initialized
{
cout << "Vulkan not supported." << endl;
abort();
}
uint32_t ext_count{}; // an output placeholder
auto ext_list = glfwGetRequiredInstanceExtensions(&ext_count);
for (int i = 0; i < ext_count; ++i) //on Windows, ext_count >=2
{
cout << ext_list[i] << endl;
}
//feed extension list into InstanceCreateInfo struct in prepare for Vulkan instance creation
vk::InstanceCreateInfo ifo{ vk::InstanceCreateFlags(), nullptr, 0, nullptr, ext_count, ext_list };
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // Comment out this if you need OpenGL also
GLFWwindow* hWnd = glfwCreateWindow(640, 480, "Foobar", NULL, NULL); // The usual window
auto inst = vk::createInstance(ifo); // Vulkan instance creation
vk::SurfaceKHR surf{};
auto psurf = VkSurfaceKHR(surf); // you can cast vk::* object to Vk* using a type cast
auto r = glfwCreateWindowSurface(VkInstance(inst), hWnd, NULL, &psurf); //VK_SUCCESS is somehow...0
// Draw stuff here
// Finish drawing
vkDestroySurfaceKHR((VkInstance)inst, VkSurfaceKHR(surf), nullptr);//vkcpp seems have no destructor for surface object
glfwDestroyWindow(hWnd); //Destroy the surface before the window
glfwTerminate(); // Free GLFW resource
return 0;
}
@MaverickTse
Copy link
Author

Note to anyone who find this gist that I wrote it many years ago, at early days of vkcpp. Now vkcpp has been renamed to vkhpp, included in Vulkan SDK by default, and probably changed its internals quite a lot. So, you should consult examples and documentation from the SDK first... If you find those official documentation difficult to comprehend, curse the KH group.(The video programming community lacks a decent writer... almost all of them just know how to write man page)

I barely do any programming anymore, so I can't comment on the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment