From b058a2c5b025ee016140d3fe2c6e9815b83654a9 Mon Sep 17 00:00:00 2001 From: itycodes Date: Mon, 12 May 2025 08:07:44 +0200 Subject: [PATCH] Cleanup + assertions + debugging --- Makefile | 2 +- main.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 05e745c..7599e52 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SHADERS = vert.spv frag.spv CC = gcc LIBS = -lvulkan -lglfw -lm -CFLAGS = $(LIBS) +CFLAGS = $(LIBS) -g OUT = main SRC = main.c diff --git a/main.c b/main.c index fb896b9..809b6e9 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,12 @@ #define MESH_SIZE 36*2*4*4 +#define ASSERT(cond, msg)\ + if(!(cond)) {\ + fprintf(stderr, "Runtime assertion %s at %s:%d failed: %s\n", #cond, __FILE__, __LINE__, msg);\ + exit(-1);\ + } + float pos_y = 0.0; float pos_z = 0.0; float pos_x = 0.0; @@ -65,7 +71,7 @@ void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) } int main() { - //glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11); + glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11); uint32_t glfw_res = glfwInit(); printf("glfwInit: (%d)\n", glfw_res); //glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); @@ -109,7 +115,8 @@ int main() { VkSurfaceKHR surface; { res = glfwCreateWindowSurface(vkins, win, NULL, &surface); - printf("CreateWindowSurface: (%ld)\n", res); + printf("CreateWindowSurface: (%018p) (%018p, %018p)\n", res, VK_ERROR_EXTENSION_NOT_PRESENT, VK_ERROR_NATIVE_WINDOW_IN_USE_KHR); + ASSERT(surface != NULL, "Failed to create GLFW Window Surface."); } VkPhysicalDevice pdev; @@ -121,6 +128,8 @@ int main() { VkPhysicalDevice* phys_devs = malloc(sizeof(VkPhysicalDevice) * num_d); res = vkEnumeratePhysicalDevices(vkins, &num_d, &phys_devs[0]); printf("EnumeratePhysicalDevices 2: (%ld) %lu\n", res, num_d); + ASSERT(num_d > 0, "Failed to detect any physical devices."); + ASSERT(res == VK_SUCCESS, "Failed to enumerate physical devices."); pdev = phys_devs[0]; } @@ -828,7 +837,7 @@ int main() { VkSwapchainKHR swapchain; { - VkSurfaceCapabilitiesKHR caps; + VkSurfaceCapabilitiesKHR caps = {}; res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(pdev, surface, &caps); printf("GetPhysicalDeviceSurfaceCapabilitiesKHR: (%ld)\n", res); printf("minImageCount: %d\n", caps.minImageCount); @@ -1135,6 +1144,7 @@ int main() { unsigned long long time_now = get_time(); printf(" \r"); fflush(stdout); + usleep(12); printf("ms per frame: %llu\r", (time_now - time_bef)/1000); fflush(stdout); }