Reputation: 39
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 build 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 works just fine.
But when i run the program i always get in the else statement.
I installed nvidia last driver, lunarg's 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
Besides i can run the cube example.
Any idea of why it never returns true ?
Upvotes: 3
Views: 2337
Reputation: 2577
You are probably missing a glfwInit()
call.
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <cassert>
int main(int argc, const char** argv) {
bool success = glfwInit(); //< Important!
assert(success);
if(glfwVVulkanSupported()) {
puts("Success!");
}
return 0;
}
Generally, the only glfw function you should call before glfwInit() is glfwSetErrorCallback
. (There may be other exceptions, but none that I am aware of.)
Upvotes: 6
Reputation: 14673
Your code is missing #define GLFW_INCLUDE_VULKAN , does it not?
#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 free to fix them. I have no way to compile it at current location
Upvotes: 2