Browse Source

Vulkan WIP

Kajetan Johannes Hammerle 9 months ago
parent
commit
0aba0d4f60
6 changed files with 70 additions and 7 deletions
  1. 1 1
      CMakeLists.txt
  2. 0 1
      cmake/clang_warnings.cmake
  3. 0 1
      cmake/gcc_warnings.cmake
  4. 67 2
      src/WindowManager.c
  5. 1 1
      tasks
  6. 1 1
      test/modules/WindowManagerTests.c

+ 1 - 1
CMakeLists.txt

@@ -60,7 +60,7 @@ target_compile_definitions(gaming_core
     PUBLIC ${DEFINITIONS}
 )
 target_link_libraries(gaming_core 
-    PRIVATE m core glfw ${LINK_OPTIONS}
+    PRIVATE m core glfw vulkan ${LINK_OPTIONS}
 )
 target_include_directories(gaming_core SYSTEM 
     PUBLIC ${CMAKE_INSTALL_PREFIX}/include

+ 0 - 1
cmake/clang_warnings.cmake

@@ -39,7 +39,6 @@ set(WARNINGS
     -Wstack-protector
     -Wstrict-overflow=2
     -Wstrict-prototypes
-    -Wswitch-enum
     -Wundef
     -Wunreachable-code
     -Wvla

+ 0 - 1
cmake/gcc_warnings.cmake

@@ -56,7 +56,6 @@ set(WARNINGS
     -Wstrict-overflow=2
     -Wstrict-prototypes
     -Wstringop-overflow=4
-    -Wswitch-enum
     -Wtrampolines
     -Wtrivial-auto-var-init
     -Wundef

+ 67 - 2
src/WindowManager.c

@@ -1,6 +1,7 @@
 #define IMPORT_CORE
 #include "core/WindowManager.h"
 
+#define GLFW_INCLUDE_VULKAN
 #include <GLFW/glfw3.h>
 #include <core/HashMap.h>
 #include <core/Logger.h>
@@ -173,6 +174,31 @@ 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", __FUNCTION__, __LINE__,    \
+                   getVulkanErrorString(vkResult));                            \
+            return true;                                                       \
+        }                                                                      \
+    } while(false)
+
 bool openWindow(const WindowOptions* o) {
     if(!glfwInit()) {
         LOG_ERROR("could not initialize GLFW");
@@ -184,6 +210,7 @@ bool openWindow(const WindowOptions* o) {
     glfwWindowHint(GLFW_RESIZABLE, true);
     glfwWindowHint(GLFW_DECORATED, !o->fullscreen);
     glfwWindowHint(GLFW_DOUBLEBUFFER, true);
+    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
 
     GLFWmonitor* m = o->fullscreen ? glfwGetPrimaryMonitor() : nullptr;
     window =
@@ -199,6 +226,46 @@ 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));
+
+    u32 graphicsFamily = 0;
+    VkPhysicalDeviceFeatures deviceFeatures = {0};
+    VkDeviceQueueCreateInfo dqInfo = {.sType =
+                                          VK_STRUCTURE_TYPE_DEVICE_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};
+    VkPhysicalDevice physicalDevice = {0};
+    VkDevice device = {0};
+    VK_ASSERT(
+        vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device));
+
     return false;
 }
 
@@ -307,7 +374,6 @@ static void checkGamepad() {
 
 static void endFrame(void) {
     sizeChanged = false;
-    glfwSwapBuffers(window);
     glfwPollEvents();
     if(searchForGamepad()) {
         checkGamepad();
@@ -342,7 +408,6 @@ static void tick() {
 }
 
 void runWindow(void) {
-    glfwMakeContextCurrent(window);
     tps.last = getNanos();
     fps.last = getNanos();
     i64 lag = 0;

+ 1 - 1
tasks

@@ -177,6 +177,6 @@ if $coverage; then
     else
         files=$(find build_debug -name *.profraw)
         llvm-profdata-16 merge -sparse $files -o build_debug/default.profdata
-        llvm-cov-16 show ./build_debug/test -instr-profile=build_debug/default.profdata --ignore-filename-regex="(test/)|(Window.c)" -line-coverage-lt=100
+        llvm-cov-16 show ./build_debug/test -instr-profile=build_debug/default.profdata --ignore-filename-regex="(test/)|(WindowManager.c)" -line-coverage-lt=100
     fi
 fi 

+ 1 - 1
test/modules/WindowManagerTests.c

@@ -6,7 +6,7 @@
 #include "../Tests.h"
 #include "core/WindowManager.h"
 
-static int ticks = 2000;
+static int ticks = 40;
 static Button closeButton = 0;
 static Button testButton = 0;
 static Button textButton = 0;