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;
}
@thestr4ng3r
Copy link

Thanks for this! I didn't know you could obtain the actual Vulkan handle by simply type casting.

By the way, you can destroy the surface by calling destroySurfaceKHR on the vk::Instance:
inst.destroySurfaceKHR(surf);
instead of
vkDestroySurfaceKHR((VkInstance)inst, VkSurfaceKHR(surf), nullptr);

@namelessvoid
Copy link

Thanks for this great gist, helped me a lot figuring out the basic glfw setup! 👍

One question: You have to copy the surface created via glfwCreateWindowSurface(..., &psurf) back to surf, don't you? E.g. surf = psurf;, since psurf does not update the handle stored in surf.

This also means that right now your call to vkDestorySurfaceKHR(...) / inst.destroySurfaceKHR(surf) does not destroy the surface created via the glfw call, does it?

@crispyricepc
Copy link

crispyricepc commented May 2, 2020

Thanks for this great gist, helped me a lot figuring out the basic glfw setup! 👍

One question: You have to copy the surface created via glfwCreateWindowSurface(..., &psurf) back to surf, don't you? E.g. surf = psurf;, since psurf does not update the handle stored in surf.

This also means that right now your call to vkDestorySurfaceKHR(...) / inst.destroySurfaceKHR(surf) does not destroy the surface created via the glfw call, does it?

Hi! Can confirm for anyone trying to do this, yes you do need to cast back. My code looks like this:

vk::SurfaceKHR surface;
/* ... */
void createSurface(vk::Instance& instance) {
    // Cast to VkSurfaceKHR for GLFW to understand
    auto vkSurface = VkSurfaceKHR(surface);
    if (glfwCreateWindowSurface(instance, glfwWindow, nullptr, &vkSurface) != VK_SUCCESS)
        throw std::runtime_error("Failed to create window surface");
    // Remember to cast back to vk::SurfaceKHR so vulkan keeps the handle
    surface = vk::SurfaceKHR(vkSurface);
}
void deleteSurface(vk::Instance& instance) {
    instance.destroySurfaceKHR(surface);
}

@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