GLFW on MSYS2 on Windows 10 and vulkan

I posted the following 2 messages on the Khronos basic coding forum and got no reply so far. May be this is a better place to get a reply. Here is the first message:
I used the command:
g++ -std=c++17 -o triangle 15_hello_triangle.cpp -lglfw3 -lvulkan -lwinmm -lgdi32 -Wl,–subsystem,windows
and it compiles and links without any error or warning messages, but when I run it, the program blank window splashes and on the console I get the message:
failed to create window surface!

Any help with what is the cause is appreciated, no I haven’t edited the source just copy paste verbatim from the web site…

And now the second one:
Well, I modified the code as follows (the function createSurface), replaced glfwCreateWindowSurface by its implementation for windows mentioned in the tutorial so it became:

void createSurface() {
	VkWin32SurfaceCreateInfoKHR createInfo = {};
	createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
	createInfo.hwnd = glfwGetWin32Window(window);
	createInfo.hinstance = GetModuleHandle(nullptr);
	if (vkCreateWin32SurfaceKHR(instance, &createInfo, nullptr,
		&surface) != VK_SUCCESS) {
			throw std::runtime_error("failed to create window surface!");		
    //if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
      //  throw std::runtime_error("failed to create window surface!");
    }
}

And of course added the necessary heading:
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#include <vulkan/vulkan_win32.h>

et voila the triangle appears in the window.

I am still wondering is this a bug in glfw and is it just for windows or also on mac and linux. Is there a better way around it? is there a patch that fixes it.

and just trying both versions on Microsoft Visual studio Community 2017 neither work.
I had to get over complaining about std::optional with a primitive type as template parameter from Microsoft compiler to get it to compile. Anyone was able to get the tutorial example working (either version) on Visual studio.

Hi and welcome to the GLFW forums!

I don’t know what tutorial you are referencing, but the triangle-vulkan.c in the GLFW test folder compiles and runs fine for me with Visual Studio 2019 on Windows 10.

You might find it useful to compile and run that example as a starting point. The easiest approach to compiling is to use cmake to generate the GLFW.sln project file for visual studio and then open that, set the startup project to triangle-vulkan in the GLFW/Tests folder and build & run that.

Cheers,

Doug.

Thanks for the reply. I’ll give your program a try but that’s C and not C++ and I don’t know yet why use glad.
The program I am talking about is C++ and is presented in this site: https://vulkan-tutorial.com/
Hope you can give it a try and let me know…

Hi,

I’m not willing to investigate and debug someone else’s tutorial - perhaps use the comment section to ask the author directly?

GLFW has a C API, so using the C program to check you can run GLFW with Vulkan is a very sensible approach. If you can build and run triangle-vulkan.c and it works properly then this will help you debug what’s going on as we will know that that version of GLFW works with Vulkan on your machine.

Cheers,

Doug.

Well the C program is 2230 line of code while the C++ is 920 line. It is much easier for a beginner to learn and understand the C++ program than the C that in addition needs an extra package glad. Also as I understand C++ is almost a super set of C, they are related…

I think you are misunderstanding me.

I am not proposing that you use the triangle-vulkan.c program as a tutorial.

I am proposing that you build and run it - if you have a problem with it then we know there is an issue with GLFW on your system.

OK, so I tried the test triangle-vulkan.c to make sure of my version of glfw3, but it needs glad/vulkan.h so I set up git to set up vcpckg to set up glad. But I only have glad.h in the include subdir and in the help of the glad.exe it mentions opengl only and there is no mention of vulkan and no vulkan.h, so I am stuck.

Another thing I don’t understand is that the installation that vcpkg created besides lacking the vulkan.h file, it says x86-windows so is it for 32-bit platform and those glad.lib are 32-bits. I am using a 64-bit platform and downloaded a 64-bit git, so how do I get glad.lib for 64 bit and also is the glad.h fine for 64-bit platform.

OK, I gave up and used cmake-gui to build glfw and it did and got everything built including the programs in tests and examples directories. Not an enlightening experience since I still do not know what I need to do to get my simple glfw vulkan program running. Anyway triangle-vulkan is running fine so apparently nothing wrong with my visual studio or glfw copy. And to do justice this 2230 line C program has a bit of animation a little more than the 920 line C++ program that has a fixed static triangle.

There were no control over cmake and it generated 32-bit versions of everything as I checked with sigcheck. Do I need to use the command line cmake to get 64-bit versions? or there is a way and still uses gui cmake.

With cmake-gui you can click on Configure and then select the generator (Visual Studio NN YYYY) and platform (Win32 or x64).

Yes thanks it worked but there is a catch it overwrote the 32-bit versions of everything including the libs. I was hoping to keep both but if I ever needed the 32-bit version I guess I’ll have to make a fresh copy of the master glfw dir

And also to make sure that my mingw64 under msys2 is also fine along with its own copy of glfw I tried to build the test program triangle-vulkan.c. It took me a while to figure out how, but by looking at the glad page at github I found out that I needed to link with -ldl on mingw to build glad.c which I obtained from the red book of opengl version 9 code, and using the command after installing libdl.a:

gcc -o triangle-vulkan triangle-vulkan.c glad.c glad_vulkan.c -lglfw3 -ldl -lvulkan -lwinmm -lgdi32 -Wl,–subsystem,windows

and the animated triangle showed up.
Conclusion: something is definitely fuzzy about how glfwCreateWindowSurface behaves and causes it to return VK_ERR while all data passed to it are fine as I checked by printing to cerr in the hell triangle.cpp tutorial program.

Since triangle-vulkan.c works but triangle 15_hello_triangle.cpp does not the first conclusion is that there might be a problem in the triangle 15_hello_triangle.cpp code.

Whilst there is potentially an issue with glfwCreateWindowSurface this is less likely as there are a large number of users of the code who have not experienced issues.

Is the full tutorial code available on github or somewhere to download? I may get time to investigate the issue, though you should also try contacting the tutorial author via their website.

If you go to this page of the tutorial you’ll find at the end of the explanatory text the c++ code and fragment and vertex shader hypertext that upon clicking on will open a page with the code that you can copy-paste into your machine:

https://vulkan-tutorial.com/en/Drawing_a_triangle/Drawing/Rendering_and_presentation

Hope you can patch this problem, I really found it much easier to grasp c++ rather than C in dealing with the colossal job of setting up vulkan…

Hope you can patch this problem

Just to clear up expectations - I may get a few minutes to help you investigate the issue but the ultimate responsibility for this code lies with it’s author, once again I suggest you contact them, there is a comment section at the bottom of the page you linked to.

Well here is another cpp program that has the same issue. I don’t think this problem is specific to this one cpp program which is well written and clear enough. I believe that it has to do with dealing with cpp that glfwCreateWindowSurface has an issue. If you go to this link:

http://www.sumantaguha.com/downloads

and download the squareVulkanized program, a program written by the author of an opengl textbook that illustrates vulkan by drawing a static square to compare it with opengl…
It has the exact same problem as the hello triangle program. First this program gives a segmentation fault, and upon replacing glfwCreateWindowSurface by the same exsact code that I used in the working version of hello triangle in mingw64, the program runs but it seems to have another issue since I get a blank window with nothing but at least the memory segmentation fault is gone.
It could be nasty problems with pointers when switching between C and C++

I was able to build the program and run it, but in my case it manages to create a surface without problems but I get an read access violation in the call to vkCreateGraphicsPipelines on line 532.

Since I didn’t create this code, it’s tricky to debug what’s going on.

Note that there is no nasty problems with pointers when switching between C and C++ .