|
@@ -1,11 +1,9 @@
|
|
|
#define IMPORT_CORE
|
|
|
#include "core/WindowManager.h"
|
|
|
|
|
|
-#define GLFW_INCLUDE_VULKAN
|
|
|
-#include <GLFW/glfw3.h>
|
|
|
-#include <core/HashMap.h>
|
|
|
#include <core/Logger.h>
|
|
|
#include <core/Utility.h>
|
|
|
+#include <core/VulkanWrapper.h>
|
|
|
#include <limits.h>
|
|
|
#include <stdio.h>
|
|
|
#include <uchar.h>
|
|
@@ -174,89 +172,6 @@ static void onMouseMove(GLFWwindow*, double x, double y) {
|
|
|
mousePosition.data[1] = (float)y;
|
|
|
}
|
|
|
|
|
|
-#define VK_ERROR_CASE(error) \
|
|
|
- case error: return #error
|
|
|
-
|
|
|
-static const char* getVulkanErrorString(VkResult r) {
|
|
|
- switch(r) {
|
|
|
- VK_ERROR_CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
|
|
|
- VK_ERROR_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
|
|
|
- VK_ERROR_CASE(VK_ERROR_INITIALIZATION_FAILED);
|
|
|
- VK_ERROR_CASE(VK_ERROR_LAYER_NOT_PRESENT);
|
|
|
- VK_ERROR_CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
|
|
|
- VK_ERROR_CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
|
|
|
- default: return "unknown";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-#define VK_ASSERT(a) \
|
|
|
- do { \
|
|
|
- VkResult vkResult = (a); \
|
|
|
- if(vkResult != VK_SUCCESS) { \
|
|
|
- printf("Vulkan error at [%s:%d]: %s\n", \
|
|
|
- getShortFileName(__FILE__), __LINE__, \
|
|
|
- getVulkanErrorString(vkResult)); \
|
|
|
- return true; \
|
|
|
- } \
|
|
|
- } while(false)
|
|
|
-
|
|
|
-static VkResult findSuitablePhysicalDevice(VkInstance instance,
|
|
|
- VkPhysicalDevice* pd) {
|
|
|
- u32 deviceCount = 0;
|
|
|
- VkResult r = vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
|
|
|
- if(r != VK_SUCCESS) {
|
|
|
- return r;
|
|
|
- } else if(deviceCount == 0) {
|
|
|
- return VK_ERROR_INITIALIZATION_FAILED;
|
|
|
- }
|
|
|
- VkPhysicalDevice* devices =
|
|
|
- cAllocate(sizeof(VkPhysicalDevice) * deviceCount);
|
|
|
- r = vkEnumeratePhysicalDevices(instance, &deviceCount, devices);
|
|
|
- if(r != VK_SUCCESS) {
|
|
|
- cFree(devices);
|
|
|
- return r;
|
|
|
- }
|
|
|
- int bestPoints = 0;
|
|
|
- LOG_INFO("Found %u devices", deviceCount);
|
|
|
- for(u32 i = 0; i < deviceCount; i++) {
|
|
|
- int points = 0;
|
|
|
- VkPhysicalDeviceProperties p;
|
|
|
- vkGetPhysicalDeviceProperties(devices[i], &p);
|
|
|
- if(p.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
|
|
|
- points += 100;
|
|
|
- } else if(p.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) {
|
|
|
- points += 50;
|
|
|
- } else if(p.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU) {
|
|
|
- points += 20;
|
|
|
- }
|
|
|
- if(points > bestPoints) {
|
|
|
- bestPoints = points;
|
|
|
- *pd = devices[i];
|
|
|
- LOG_INFO("Best Device: %s", p.deviceName);
|
|
|
- }
|
|
|
- }
|
|
|
- cFree(devices);
|
|
|
- return bestPoints > 0 ? VK_SUCCESS : VK_ERROR_INITIALIZATION_FAILED;
|
|
|
-}
|
|
|
-
|
|
|
-static u32 findQueueFamilies(VkPhysicalDevice pd, VkQueueFlags flags) {
|
|
|
- u32 count = 0;
|
|
|
- vkGetPhysicalDeviceQueueFamilyProperties(pd, &count, nullptr);
|
|
|
- VkQueueFamilyProperties* properties =
|
|
|
- cAllocate(sizeof(VkQueueFamilyProperties) * count);
|
|
|
- vkGetPhysicalDeviceQueueFamilyProperties(pd, &count, properties);
|
|
|
- for(u32 i = 0; i < count; i++) {
|
|
|
- if(properties[i].queueCount != 0 &&
|
|
|
- (properties[i].queueFlags & flags) == flags) {
|
|
|
- LOG_INFO("Queue family bits: %x", properties[i].queueFlags);
|
|
|
- cFree(properties);
|
|
|
- return i;
|
|
|
- }
|
|
|
- }
|
|
|
- cFree(properties);
|
|
|
- return (u32)-1;
|
|
|
-}
|
|
|
-
|
|
|
bool openWindow(const WindowOptions* o) {
|
|
|
if(!glfwInit()) {
|
|
|
LOG_ERROR("could not initialize GLFW");
|
|
@@ -284,59 +199,12 @@ bool openWindow(const WindowOptions* o) {
|
|
|
glfwSetFramebufferSizeCallback(window, onResize);
|
|
|
glfwSetMouseButtonCallback(window, onMouse);
|
|
|
glfwSetCursorPosCallback(window, onMouseMove);
|
|
|
-
|
|
|
- VkInstance instance = {0};
|
|
|
- VkApplicationInfo info = {.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
|
|
- .pApplicationName = "Vulkan",
|
|
|
- .applicationVersion = VK_MAKE_VERSION(1, 0, 0),
|
|
|
- .pEngineName = "Kajetan",
|
|
|
- .engineVersion = VK_MAKE_VERSION(0, 0, 1),
|
|
|
- .apiVersion = VK_API_VERSION_1_1};
|
|
|
- VkInstanceCreateInfo instanceCreateInfo = {
|
|
|
- .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
|
|
- .pApplicationInfo = &info,
|
|
|
- .enabledLayerCount = 1,
|
|
|
- .ppEnabledLayerNames = (const char*[]){"VK_LAYER_KHRONOS_validation"},
|
|
|
- .enabledExtensionCount = 4,
|
|
|
- .ppEnabledExtensionNames =
|
|
|
- (const char*[]){"VK_KHR_surface", "VK_KHR_xcb_surface",
|
|
|
- VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
|
|
|
- VK_EXT_DEBUG_REPORT_EXTENSION_NAME}};
|
|
|
- VK_ASSERT(vkCreateInstance(&instanceCreateInfo, nullptr, &instance));
|
|
|
-
|
|
|
- VkPhysicalDevice physicalDevice = {0};
|
|
|
- VK_ASSERT(findSuitablePhysicalDevice(instance, &physicalDevice));
|
|
|
- VkPhysicalDeviceFeatures deviceFeatures = {0};
|
|
|
- vkGetPhysicalDeviceFeatures(physicalDevice, &deviceFeatures);
|
|
|
-
|
|
|
- u32 graphicsFamily =
|
|
|
- findQueueFamilies(physicalDevice, VK_QUEUE_GRAPHICS_BIT);
|
|
|
- if(graphicsFamily == (u32)-1) {
|
|
|
- LOG_ERROR("Cannot find queue family");
|
|
|
- return true;
|
|
|
- }
|
|
|
- VkDeviceQueueCreateInfo dqInfo = {
|
|
|
- .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
|
|
- .queueFamilyIndex = graphicsFamily,
|
|
|
- .queueCount = 1,
|
|
|
- .pQueuePriorities = (float[]){1.0f}};
|
|
|
- VkDeviceCreateInfo deviceCreateInfo = {
|
|
|
- .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
|
|
- .queueCreateInfoCount = 1,
|
|
|
- .pQueueCreateInfos = &dqInfo,
|
|
|
- .enabledExtensionCount = 1,
|
|
|
- .ppEnabledExtensionNames =
|
|
|
- (const char*[]){VK_KHR_SWAPCHAIN_EXTENSION_NAME},
|
|
|
- .pEnabledFeatures = &deviceFeatures};
|
|
|
- VkDevice device = {0};
|
|
|
- VK_ASSERT(
|
|
|
- vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device));
|
|
|
-
|
|
|
- return false;
|
|
|
+ return coreInitVulkan();
|
|
|
}
|
|
|
|
|
|
void closeWindow(void) {
|
|
|
if(window != nullptr) {
|
|
|
+ coreDestroyVulkan();
|
|
|
glfwDestroyWindow(window);
|
|
|
window = nullptr;
|
|
|
}
|