2020-02-03 23:23:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-06-17 23:13:39 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <fcntl.h>
|
|
|
|
#define NOMINMAX
|
|
|
|
#include <windows.h>
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2020-02-03 23:23:47 +00:00
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
|
|
|
#include "../src/VkBootstrap.h"
|
2022-10-07 18:19:29 +00:00
|
|
|
#include "../src/VkSwapchainManager.h"
|
|
|
|
|
|
|
|
const int default_window_width = 512;
|
|
|
|
const int default_window_height = 512;
|
2020-02-03 23:23:47 +00:00
|
|
|
|
2021-03-01 22:21:45 +00:00
|
|
|
GLFWwindow* create_window_glfw(const char* window_name = "", bool resize = true) {
|
|
|
|
glfwInit();
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
2022-10-07 18:19:29 +00:00
|
|
|
glfwWindowHint(GLFW_RESIZABLE, resize ? GLFW_TRUE : GLFW_FALSE);
|
2020-02-04 03:34:46 +00:00
|
|
|
|
2022-10-07 18:19:29 +00:00
|
|
|
return glfwCreateWindow(default_window_width, default_window_height, window_name, NULL, NULL);
|
2020-02-03 23:23:47 +00:00
|
|
|
}
|
2021-03-01 22:21:45 +00:00
|
|
|
void destroy_window_glfw(GLFWwindow* window) {
|
|
|
|
glfwDestroyWindow(window);
|
|
|
|
glfwTerminate();
|
2020-02-03 23:23:47 +00:00
|
|
|
}
|
2022-10-07 18:19:29 +00:00
|
|
|
VkSurfaceKHR create_surface_glfw(VkInstance instance, GLFWwindow* window, VkAllocationCallbacks* allocator = nullptr) {
|
2021-03-01 22:32:26 +00:00
|
|
|
VkSurfaceKHR surface = VK_NULL_HANDLE;
|
2021-06-06 20:02:02 +00:00
|
|
|
VkResult err = glfwCreateWindowSurface(instance, window, allocator, &surface);
|
2020-02-18 22:18:08 +00:00
|
|
|
if (err) {
|
2020-02-03 23:23:47 +00:00
|
|
|
const char* error_msg;
|
2021-03-01 22:21:45 +00:00
|
|
|
int ret = glfwGetError(&error_msg);
|
2020-02-18 22:18:08 +00:00
|
|
|
if (ret != 0) {
|
2020-02-03 23:23:47 +00:00
|
|
|
std::cout << ret << " ";
|
|
|
|
if (error_msg != nullptr) std::cout << error_msg;
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
2021-03-01 22:32:26 +00:00
|
|
|
surface = VK_NULL_HANDLE;
|
2020-02-03 23:23:47 +00:00
|
|
|
}
|
|
|
|
return surface;
|
2020-02-18 22:18:08 +00:00
|
|
|
}
|
2020-06-17 23:13:39 +00:00
|
|
|
|
|
|
|
struct VulkanLibrary {
|
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
|
|
void* library;
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
|
|
|
HMODULE library;
|
|
|
|
#endif
|
|
|
|
|
2021-03-01 22:21:45 +00:00
|
|
|
VulkanLibrary() {
|
2020-06-17 23:13:39 +00:00
|
|
|
#if defined(__linux__)
|
2021-03-01 22:21:45 +00:00
|
|
|
library = dlopen("libvulkan.so.1", RTLD_NOW | RTLD_LOCAL);
|
|
|
|
if (!library) library = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
|
2020-06-17 23:13:39 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-03-01 22:21:45 +00:00
|
|
|
library = dlopen("libvulkan.dylib", RTLD_NOW | RTLD_LOCAL);
|
|
|
|
if (!library) library = dlopen("libvulkan.1.dylib", RTLD_NOW | RTLD_LOCAL);
|
2020-06-17 23:13:39 +00:00
|
|
|
#elif defined(_WIN32)
|
2021-03-01 22:21:45 +00:00
|
|
|
library = LoadLibrary(TEXT("vulkan-1.dll"));
|
2020-06-17 23:13:39 +00:00
|
|
|
#else
|
2021-03-01 22:21:45 +00:00
|
|
|
assert(false && "Unsupported platform");
|
2020-06-17 23:13:39 +00:00
|
|
|
#endif
|
|
|
|
if (!library) return;
|
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
2022-10-07 18:19:29 +00:00
|
|
|
vkGetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(dlsym(library, "vkGetInstanceProcAddr"));
|
2020-06-17 23:13:39 +00:00
|
|
|
#elif defined(_WIN32)
|
2022-10-07 18:19:29 +00:00
|
|
|
vkGetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(GetProcAddress(library, "vkGetInstanceProcAddr"));
|
2020-06-17 23:13:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-01 22:21:45 +00:00
|
|
|
void close() {
|
2020-06-17 23:13:39 +00:00
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
2021-03-01 22:21:45 +00:00
|
|
|
dlclose(library);
|
2020-06-17 23:13:39 +00:00
|
|
|
#elif defined(_WIN32)
|
2021-03-01 22:21:45 +00:00
|
|
|
FreeLibrary(library);
|
2020-06-17 23:13:39 +00:00
|
|
|
#endif
|
|
|
|
library = 0;
|
|
|
|
}
|
2021-03-01 22:21:45 +00:00
|
|
|
|
|
|
|
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = VK_NULL_HANDLE;
|
2020-06-17 23:13:39 +00:00
|
|
|
};
|