Vulkan and glfw - glfwVulkanSupported () always return false
I'm trying to run a basic Vulkan test using GLFW3.2:
#include <vulkan/vulkan.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main(int argc, char *argv[])
{
if (glfwVulkanSupported())
{
std::cout << "vulkan supported !" << std::endl;
}
else
{
std::cout << "vulkan NOT supported !" << std::endl;
}
return 0;
}
Which I am creating using the following cmake:
cmake_minimum_required(VERSION 3.5.1)
project(vktest)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.9")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Vulkan REQUIRED)
find_package(glfw3 3.2 REQUIRED)
include_directories(${Vulkan_INCLUDE_DIRS})
message("kek")
message(${Vulkan_INCLUDE_DIRS})
message(${Vulkan_LIBRARIES})
message(${Vulkan_LIBRARY})
add_executable(vktest vulkanTest.cpp)
target_link_libraries(vktest ${Vulkan_LIBRARIES})
target_link_libraries(vktest glfw)
Building this is fine.
But when I run the program, I always get the else statement.
I installed the latest nvidia driver, lunarg vulkan sdk with the following environment variables:
export VULKAN_SDK=/home/mathias/vulkan/VulkanSDK/1.0.42.2/x86_64
set PATH $VULKAN_SDK/bin $PATH
set LD_LIBRARY_PATH $VULKAN_SDK/lib $LD_LIBRARY_PATH
set VK_LAYER_PATH $VULKAN_SDK/etc/explicit_layer.d
Alternatively, I can run the example cube.
Any idea as to why it never returns true?
source to share
The #define GLFW_INCLUDE_VULKAN is missing in the code, isn't it?
#define GLFW_INCLUDE_VULKAN //as required by GLFW
#include "vulkan\vk_cpp.hpp"
#include "GLFW\glfw3.h"
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
if (!glfwInit()) //Initialize GLFW
{
std::cout << "GLFW not initialized.\n";
abort();
}
if (!glfwVulkanSupported()) //Any Vulkan-related function requires GLFW initialized
{
std::cout << "Vulkan not supported.\n";
abort();
}
uint32_t ext_count; // an output placeholder
auto ext_list = glfwGetRequiredInstanceExtensions(&ext_count);
std::copy(&ext_list[0], &ext_list[ext_count-1], std::ostream_iterator<const char *>(cout, "\n"));
//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;
}
if there are typos in this code, you can correct them. I have no way to compile it at the current location
source to share
You probably don't need a challenge glfwInit()
.
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <cassert>
int main(int argc, const char** argv) {
assert(glfwInit()); //< Important!
if(glfwVVulkanSupported()) {
puts("Success!");
}
return 0;
}
Typically, the only glfw function you should call before glfwInit () is glfwSetErrorCallback
. (There may be other exceptions, but I am not aware of this.)
source to share