From 9bbc581716d7a7de1e2e11779d2d609944379576 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sun, 19 Dec 2021 09:23:03 +0200 Subject: [PATCH] foo --- src/app.c | 5 ++ src/main_x11.c | 5 ++ src/util.c | 43 +++++++++++++ src/util.h | 31 ++++++++++ src/vk.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/vk.h | 7 +++ 6 files changed, 280 insertions(+) create mode 100644 src/util.c create mode 100644 src/util.h create mode 100644 src/vk.c create mode 100644 src/vk.h diff --git a/src/app.c b/src/app.c index 9be52ca..4d845c5 100644 --- a/src/app.c +++ b/src/app.c @@ -1,12 +1,17 @@ #include "app.h" +#include "vk.h" int app_init(void) { + if(vk_init() == -1) { + return -1; + } return 0; } void app_cleanup(void) { + vk_cleanup(); } diff --git a/src/main_x11.c b/src/main_x11.c index 54f4df6..d593494 100644 --- a/src/main_x11.c +++ b/src/main_x11.c @@ -32,6 +32,10 @@ int main(int argc, char **argv) return 1; } + if(app_init() == -1) { + goto end; + } + for(;;) { while(XPending(dpy)) { XNextEvent(dpy, &ev); @@ -46,6 +50,7 @@ int main(int argc, char **argv) } end: + app_cleanup(); XDestroyWindow(dpy, win); XCloseDisplay(dpy); return 0; diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..dc86d10 --- /dev/null +++ b/src/util.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include "util.h" + +void *malloc_nf_impl(size_t sz, const char *file, int line) +{ + void *p; + if(!(p = malloc(sz))) { + fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)sz); + abort(); + } + return p; +} + +void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line) +{ + void *p; + if(!(p = calloc(num, sz))) { + fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)(sz * num)); + abort(); + } + return p; +} + +void *realloc_nf_impl(void *p, size_t sz, const char *file, int line) +{ + if(!(p = realloc(p, sz))) { + fprintf(stderr, "%s:%d failed to realloc %lu bytes\n", file, line, (unsigned long)sz); + abort(); + } + return p; +} + +char *strdup_nf_impl(const char *s, const char *file, int line) +{ + char *res; + if(!(res = strdup(s))) { + fprintf(stderr, "%s:%d failed to duplicate string\n", file, line); + abort(); + } + return res; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..61ae532 --- /dev/null +++ b/src/util.h @@ -0,0 +1,31 @@ +#ifndef UTIL_H_ +#define UTIL_H_ + +#include +#include + +#if defined(__WATCOMC__) || defined(WIN32) +#include +#else +#if !defined(__FreeBSD__) && !defined(__OpenBSD__) +#include +#endif +#endif + +#ifdef _MSC_VER +#define strcasecmp(s, k) stricmp(s, k) +#endif + +#define malloc_nf(sz) malloc_nf_impl(sz, __FILE__, __LINE__) +void *malloc_nf_impl(size_t sz, const char *file, int line); + +#define calloc_nf(num, sz) calloc_nf_impl(num, sz, __FILE__, __LINE__) +void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line); + +#define realloc_nf(p, sz) realloc_nf_impl(p, sz, __FILE__, __LINE__) +void *realloc_nf_impl(void *p, size_t sz, const char *file, int line); + +#define strdup_nf(s) strdup_nf_impl(s, __FILE__, __LINE__) +char *strdup_nf_impl(const char *s, const char *file, int line); + +#endif /* UTIL_H_ */ diff --git a/src/vk.c b/src/vk.c new file mode 100644 index 0000000..1fcba58 --- /dev/null +++ b/src/vk.c @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include "vk.h" +#include "util.h" + +static int create_instance(void); +static int create_device(void); +static int have_inst_layer(const char *name); +static int have_inst_ext(const char *name); +static int have_dev_ext(const char *name); + +static VkInstance vk; + +static VkLayerProperties *inst_layers; +static VkExtensionProperties *inst_ext, *dev_ext; +static uint32_t inst_ext_count, dev_ext_count, inst_layers_count; + +static VkPhysicalDevice *pdev_list; +static uint32_t num_pdev; + +static int have_debug_report; + +#define ARRSZ(arr) (sizeof arr / sizeof *arr) +static const char *known_layer_list[] = { + "VK_LAYER_GOOGLE_threading", + "VK_LAYER_LUNARG_parameter_validation", + "VK_LAYER_LUNARG_object_tracker", + "VK_LAYER_LUNARG_image", + "VK_LAYER_LUNARG_core_validation", + "VK_LAYER_LUNARG_swapchain", + "VK_LAYER_GOOGLE_unique_objects" +}; + +static struct { + const char *name; + int required; +} known_ext_list[] = { + {"VK_KHR_surface", 1}, +#ifdef __WIN32__ + {"VK_KHR_win32_surface", 1}, +#else + {"VK_KHR_xlib_surface", 1}, +#endif + {"VK_KHR_debug_report", 0}, + {"VK_KHR_acceleration_structure", 0}, + {"VK_KHR_ray_tracing_pipeline", 0} +}; + +int vk_init(void) +{ + if(create_instance() == -1) { + return -1; + } + if(create_device() == -1) { + return -1; + } + return 0; +} + +void vk_cleanup(void) +{ + vkDestroyInstance(vk, 0); + free(inst_layers); + free(inst_ext); + free(pdev_list); +} + + +static int create_instance(void) +{ + int i, nlayers = 0, next = 0; + VkInstanceCreateInfo instinf; + VkApplicationInfo appinf; + const char *layers[ARRSZ(known_layer_list)]; + const char *ext[ARRSZ(known_ext_list)]; + uint32_t apiver; + + vkEnumerateInstanceVersion(&apiver); + printf("Vulkan API version: %d.%d.%d\n", (apiver >> 22) & 0x7f, + (apiver >> 12) & 0x3ff, apiver & 0xfff); + + memset(&appinf, 0, sizeof appinf); + appinf.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + appinf.pApplicationName = "vkray"; + appinf.pEngineName = "vkray"; + appinf.apiVersion = apiver; + + vkEnumerateInstanceLayerProperties(&inst_layers_count, 0); + inst_layers = malloc_nf(inst_layers_count * sizeof *inst_layers); + vkEnumerateInstanceLayerProperties(&inst_layers_count, inst_layers); + + vkEnumerateInstanceExtensionProperties(0, &inst_ext_count, 0); + inst_ext = malloc_nf(inst_ext_count * sizeof *inst_ext); + vkEnumerateInstanceExtensionProperties(0, &inst_ext_count, inst_ext); + + printf("Layers:\n"); + for(i=0; i