uninit branch fix in vk.c and added win32 codepaths
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 1 Sep 2024 20:37:53 +0000 (23:37 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 1 Sep 2024 20:37:53 +0000 (23:37 +0300)
src/vk.c
src/vk.h

index 3ea0060..a18a0cd 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -9,7 +9,8 @@
 #include "util.h"
 #include "darray.h"
 
-#ifdef __WIN32__
+#ifdef _WIN32
+#include <windows.h>
 #include <vulkan/vulkan_win32.h>
 #else
 /*#include <vulkan/vulkan_xlib.h>*/
@@ -90,8 +91,13 @@ static int eval_pdev_score(VkPhysicalDevice dev);
 static int have_inst_layer(const char *name);
 static int have_ext(VkExtensionProperties *ext, int next, const char *name);
 
+#ifdef _WIN32
+static HINSTANCE hinst;
+static HWND win;
+#else
 static Display *dpy;
 static Window win;
+#endif
 static int initflags;
 #define MAX_INIT_QUEUE 32
 static struct {
@@ -127,11 +133,19 @@ static uint32_t num_pdev;
 
 static int have_raytrace, have_debug_report;
 
+#ifdef _WIN32
+void vk_init_win(HINSTANCE hi, HWND w)
+{
+       hinst = hi;
+       win = w;
+}
+#else
 void vk_init_xwin(Display *d, Window w)
 {
        dpy = d;
        win = w;
 }
+#endif
 
 void vk_init_queue(unsigned int qflags, int count)
 {
@@ -165,13 +179,13 @@ int vk_init(unsigned int flags, unsigned int *usedflags)
        if(choose_phys_dev() == -1) return -1;
        if(create_device() == -1) return -1;
 
-       if(initflags != flags) {
-               if(usedflags) {
-                       *usedflags = initflags;
-               } else {
-                       vk_cleanup();
-                       return -1;
-               }
+       if(initflags != flags && !usedflags) {
+               vk_cleanup();
+               return -1;
+       }
+
+       if(usedflags) {
+               *usedflags = initflags;
        }
        return 0;
 }
@@ -1099,7 +1113,7 @@ static struct {
        int required;
 } known_instext_list[] = {
        {"VK_KHR_surface", 1},
-#ifdef __WIN32__
+#ifdef _WIN32
        {"VK_KHR_win32_surface", 1},
 #else
        /*{"VK_KHR_xlib_surface", 1},*/
@@ -1114,6 +1128,7 @@ static struct {
 } known_devext_list[] = {
        {"VK_KHR_swapchain", 1},
        {"VK_KHR_acceleration_structure", 0},
+       {"VK_KHR_deferred_host_operations", 0},
        {"VK_KHR_ray_tracing_pipeline", 0}
 };
 
@@ -1187,6 +1202,17 @@ static int create_instance(void)
 
 static int create_surface(void)
 {
+#ifdef _WIN32
+       VkWin32SurfaceCreateInfoKHR winf = {0};
+       winf.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
+       winf.hinstance = hinst;
+       winf.hwnd = win;
+
+       if(vkCreateWin32SurfaceKHR(vk, &winf, 0, &vksurf) != 0) {
+               fprintf(stderr, "failed to create win32 window surface\n");
+               return -1;
+       }
+#else
        /*
        VkXlibSurfaceCreateInfoKHR xinf = {0};
        xinf.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
@@ -1207,6 +1233,7 @@ static int create_surface(void)
                fprintf(stderr, "failed to create XCB window surface\n");
                return -1;
        }
+#endif
        return 0;
 }
 
@@ -1282,8 +1309,10 @@ static int create_device(void)
 
        if(initflags & VKINIT_RAY) {
                if(have_ext(dev_ext, dev_ext_count, "VK_KHR_acceleration_structure") &&
+                               have_ext(dev_ext, dev_ext_count, "VK_KHR_deferred_host_operations") &&
                                have_ext(dev_ext, dev_ext_count, "VK_KHR_ray_tracing_pipeline")) {
                        ext[num_ext++] = "VK_KHR_acceleration_structure";
+                       ext[num_ext++] = "VK_KHR_deferred_host_operations";
                        ext[num_ext++] = "VK_KHR_ray_tracing_pipeline";
                } else {
                        initflags &= ~VKINIT_RAY;
@@ -1362,7 +1391,7 @@ static int create_swapchain(void)
                return -1;
        }
 
-       if(!vksc_img || vksc_numimg != num) {
+       if(!vksc_img) {
                free(vksc_img);
                vkGetSwapchainImagesKHR(vkdev, vksc, &num, 0);
                vksc_img = malloc_nf(num * sizeof *vksc_img);
index b9a8fb0..accc10c 100644 (file)
--- a/src/vk.h
+++ b/src/vk.h
@@ -1,7 +1,11 @@
 #ifndef VK_H_
 #define VK_H_
 
+#ifdef _WIN32
+#include <windows.h>
+#else
 #include <X11/Xlib.h>
+#endif
 #include <vulkan/vulkan.h>
 
 enum {
@@ -49,7 +53,11 @@ enum {
        VKPRIM_TRIANGLES        = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
 };
 
+#ifdef _WIN32
+void vk_init_win(HINSTANCE hinst, HWND win);
+#else
 void vk_init_xwin(Display *dpy, Window win);
+#endif
 void vk_init_queue(unsigned int qflags, int count);
 
 int vk_init(unsigned int flags, unsigned int *usedflags);