Created
February 26, 2021 03:41
-
-
Save DavidEGrayson/2f868766cfaf5717279ce8b2455974d3 to your computer and use it in GitHub Desktop.
Test code for Vulkan loader issues in MSYS2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile with: gcc -g bad.c -lvulkan -lgdi32 -lglfw3 | |
#define VK_USE_PLATFORM_WIN32_KHR | |
#include <vulkan/vulkan.h> | |
#define GLFW_EXPOSE_NATIVE_WIN32 | |
#include <GLFW/glfw3.h> | |
#include <GLFW/glfw3native.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
void error_callback(int error, const char* description) { | |
printf("GLFW error: %s\n", description); | |
fflush(stdout); | |
} | |
int main() { | |
if (!glfwInit()) { | |
fprintf(stderr, "GLFW init failed!\n"); | |
exit(1); | |
} | |
if (!glfwVulkanSupported()) { | |
fprintf(stderr, "GLFW does not support Vulkan!\n"); | |
exit(1); | |
} | |
glfwSetErrorCallback(error_callback); | |
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); | |
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); | |
GLFWwindow * window = glfwCreateWindow(800, 600, "Vulkan", NULL, NULL); | |
VkInstanceCreateInfo createInfo = {}; | |
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; | |
createInfo.ppEnabledExtensionNames = | |
glfwGetRequiredInstanceExtensions(&createInfo.enabledExtensionCount); | |
const char * layers[] = { "VK_LAYER_KHRONOS_validation" }; | |
createInfo.ppEnabledLayerNames = layers; | |
createInfo.enabledLayerCount = 0; // change to 1 to request validation | |
for (uint32_t i = 0; i < createInfo.enabledExtensionCount; i++) { | |
printf("Extension: %s\n", createInfo.ppEnabledExtensionNames[i]); | |
} | |
VkResult vkr; | |
VkInstance instance = 0; | |
vkr = vkCreateInstance(&createInfo, NULL, &instance); | |
if (vkr != VK_SUCCESS) { | |
fprintf(stderr, "Failed to create instance (code %d)!\n", vkr); | |
exit(1); | |
} | |
VkSurfaceKHR surface = 0; | |
if (1) // change to 0 to use glfwCreateWindowSurface | |
{ | |
// Using Vulkan directly to create the surface does work. | |
VkWin32SurfaceCreateInfoKHR surfaceInfo = {}; | |
surfaceInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; | |
surfaceInfo.hinstance = GetModuleHandle(NULL); | |
surfaceInfo.hwnd = glfwGetWin32Window(window); | |
vkr = vkCreateWin32SurfaceKHR(instance, &surfaceInfo, NULL, &surface); | |
} | |
else | |
{ | |
// Using glfw to create the surface doesn't work, resulting in the output: | |
// GLFW error: Win32: Vulkan instance missing VK_KHR_win32_surface extension | |
// Failed to create window surface. | |
// Extension not present. | |
vkr = glfwCreateWindowSurface(instance, window, NULL, &surface); | |
} | |
if (vkr != VK_SUCCESS) { | |
fprintf(stderr, "Failed to create window surface.\n"); | |
if (vkr == VK_ERROR_EXTENSION_NOT_PRESENT) { | |
fprintf(stderr, "Extension not present.\n"); | |
} | |
exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment