From 6c31a3ce270d12b16170e406c5ec3a441bd2dbcf Mon Sep 17 00:00:00 2001 From: Lorenzo Torres Date: Mon, 3 Nov 2025 13:08:46 +0100 Subject: [PATCH] added vulkan device and queues creation --- config.mk | 2 +- core/arena.c | 6 ++++ core/arena.h | 1 + core/log.h | 3 +- core/vector.c | 1 + makefile | 6 ++-- rendering/renderer.h | 14 +++++++- topaz.c | 13 ++++++-- types.h | 2 ++ vk/device.c | 60 ++++++++++++++++++++++++++++++++++ vk/device.h | 10 ++++++ vk/instance.c | 16 ++++----- vk/instance.h | 2 ++ vk/physical_device.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ vk/physical_device.h | 29 +++++++++++++++++ vk/platform.c | 1 + vk/renderer.c | 16 +++++++-- vk/vk.h | 10 ++++++ world/block.h | 1 + world/chunk.h | 1 + 20 files changed, 253 insertions(+), 18 deletions(-) create mode 100644 vk/device.c create mode 100644 vk/device.h create mode 100644 vk/physical_device.h diff --git a/config.mk b/config.mk index a4724b6..6689e7b 100644 --- a/config.mk +++ b/config.mk @@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause CC := cc -CFLAGS := -Wall -Wextra -std=c99 -pedantic -ggdb -O2 -DDEBUG +CFLAGS := -Wall -Wextra -std=c99 -pedantic -ggdb -fsanitize=address -DDEBUG LIBS := -lm # can be gl or vk BACKEND := vk diff --git a/core/arena.c b/core/arena.c index f383155..ae6d188 100644 --- a/core/arena.c +++ b/core/arena.c @@ -2,6 +2,7 @@ #include "arena.h" #include + struct arena_allocator *arena_init(usize size) { struct arena_allocator *allocator = (struct arena_allocator *) malloc(sizeof(struct arena_allocator)); @@ -20,8 +21,13 @@ void arena_deinit(struct arena_allocator *allocator) void *arena_alloc(struct arena_allocator *allocator, usize size) { + if (allocator->position + size >= allocator->size) { + allocator->size = allocator->position + size; + allocator->base = (usize) realloc((void *)allocator->base, allocator->size + allocator->size / 2); + } void *ptr = (void *)(allocator->base + allocator->position); allocator->position += size; + return ptr; } diff --git a/core/arena.h b/core/arena.h index 9d2b0fa..66a3b3f 100644 --- a/core/arena.h +++ b/core/arena.h @@ -17,6 +17,7 @@ struct arena_allocator { usize position; }; +extern struct arena_allocator *global_arena; /* Create a new arena allocator of size `size` */ struct arena_allocator *arena_init(usize size); diff --git a/core/log.h b/core/log.h index fdb0a2a..7b02a97 100644 --- a/core/log.h +++ b/core/log.h @@ -4,6 +4,7 @@ #include #include +#include "arena.h" #define log_error_code(msg) do { printf("\x1b[31m" "[ERROR]" "\x1b[0m" "(" __FILE__ ":" __LINE__ "): %s", (msg)); } while (0) #define log_warning_code(msg) do { printf("\x1b[33m" "[WARN]" "\x1b[0m" "(" __FILE__ ":" __LINE__ "): %s", (msg)); } while (0) @@ -13,6 +14,6 @@ #define log_warning(msg) do { printf("\x1b[33m" "[WARN]" "\x1b[0m: %s", (msg)); } while (0) #define log_info(msg) do { printf("\x1b[32m" "[INFO]" "\x1b[0m: %s", (msg)); } while (0) -#define fatal(msg) do { log_error(msg); exit(1); } while (0) +#define fatal(msg) do { arena_bump(global_arena); arena_deinit(global_arena); log_error(msg); exit(1); } while (0) #endif diff --git a/core/vector.c b/core/vector.c index 6c1b3a6..a6e7da6 100644 --- a/core/vector.c +++ b/core/vector.c @@ -4,6 +4,7 @@ struct vector *vector_init(usize size, usize element_size) { + if (size == 0) size = 1; struct vector *vector = (struct vector *) malloc(sizeof(struct vector)); vector->length = 0; vector->size = size; diff --git a/makefile b/makefile index 4eab902..02900d7 100644 --- a/makefile +++ b/makefile @@ -15,13 +15,15 @@ endif ifeq (${BACKEND},vk) SRC += vk/platform.c\ vk/renderer.c\ - vk/instance.c + vk/instance.c\ + vk/physical_device.c\ + vk/device.c endif OBJ:=${SRC:.c=.o} all: ${OBJ} - ${CC} ${LIBS} ${OBJ} -o topaz + ${CC} ${LIBS} ${CFLAGS} ${OBJ} -o topaz %.o: %.c ${CC} ${CFLAGS} -c $< -o $@ diff --git a/rendering/renderer.h b/rendering/renderer.h index 7e59dd4..6f8dd83 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -4,14 +4,26 @@ #define RENDERER_H #include "../types.h" +/* + * A mesh is a drawable object represented + * as an index (offset) in the global vertex + * and index buffers and a size. + */ struct mesh { - usize index; + usize vertex_offset; + usize index_offset; usize size; }; +/* The renderer context stores objects + * related to rendering. Implementation + * depends on the graphics backend used + * so for reference see gl/gl.h or vk/vk.h + */ struct renderer_context; struct renderer_context *renderer_context_init(void); +void renderer_context_deinit(struct renderer_context *context); struct mesh *renderer_build_chunk_mesh(void); void renderer_draw_mesh(struct mesh mesh); void renderer_draw_chunk(struct mesh mesh); diff --git a/topaz.c b/topaz.c index bdf4d41..629c749 100644 --- a/topaz.c +++ b/topaz.c @@ -1,10 +1,17 @@ // SPDX-License-Identifier: BSD-3-Clause #include -#include "core/vector.h" #include "platform.h" +#include "core/arena.h" +#include "types.h" -int main(int argc, char **argv) +struct arena_allocator *global_arena = NULL; + +i32 main(i32 argc, char **argv) { - return platform_run(argc, argv); + global_arena = arena_init(1); + u32 result = platform_run(argc, argv); + arena_bump(global_arena); + arena_deinit(global_arena); + return result; } diff --git a/types.h b/types.h index 4392fd8..f382d43 100644 --- a/types.h +++ b/types.h @@ -20,4 +20,6 @@ typedef size_t usize; typedef float f32; typedef double f64; +typedef uint8_t bool; + #endif diff --git a/vk/device.c b/vk/device.c new file mode 100644 index 0000000..f99dc6e --- /dev/null +++ b/vk/device.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: BSD-3-Clause +#include "device.h" +#include "physical_device.h" +#include "../core/vector.h" +#include "../core/log.h" + +#include + +void vk_device_init(struct renderer_context *context) +{ + struct vector *device_extensions = vector_init(0, sizeof(char *)); + struct vector *physical_device_extensions = vk_physical_device_get_extensions(context); + for (usize i = 0; i < physical_device_extensions->length; i++) { + if (strcmp(((char **)physical_device_extensions->data)[i], "VK_KHR_portability_subset") == 0) { + /* + * The spec states that if VK_KHR_portability_subset + * is present in the physical device extensions, + * the device should also have that extension enabled. + */ + vector_push(device_extensions, char *, "VK_KHR_portability_subset"); + } + + free(((char **)physical_device_extensions->data)[i]); + } + + f32 priority = 0.0; + VkDeviceQueueCreateInfo graphics_queue_create_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = context->queue_indices.graphics, + .queueCount = 1, + .pQueuePriorities = &priority + }; + + VkDeviceCreateInfo device_create_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = NULL, + .flags = 0, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &graphics_queue_create_info, + .enabledLayerCount = 0, + .ppEnabledLayerNames = NULL, + .enabledExtensionCount = device_extensions->length, + .ppEnabledExtensionNames = (const char **)device_extensions->data, + .pEnabledFeatures = NULL, + }; + + if (vkCreateDevice(context->physical_device, &device_create_info, NULL, &context->device) != VK_SUCCESS) { + fatal("Can't create Vulkan device.\n"); + } + + vkGetDeviceQueue(context->device, graphics_queue_create_info.queueFamilyIndex, 0, &context->graphics_queue); + + vector_deinit(physical_device_extensions); + vector_deinit(device_extensions); +} + +void vk_device_deinit(struct renderer_context *context) +{ + vkDestroyDevice(context->device, NULL); +} diff --git a/vk/device.h b/vk/device.h new file mode 100644 index 0000000..60d7430 --- /dev/null +++ b/vk/device.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause +#ifndef DEVICE_H +#define DEVICE_H + +#include "vk.h" + +void vk_device_init(struct renderer_context *context); +void vk_device_deinit(struct renderer_context *context); + +#endif diff --git a/vk/instance.c b/vk/instance.c index 25340d2..1ec7a21 100644 --- a/vk/instance.c +++ b/vk/instance.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #include "instance.h" #include "../core/log.h" #define RGFW_VULKAN @@ -40,27 +41,19 @@ const char **vk_instance_extensions(usize *count) *count = rgfw_extension_count + extension_count; const char **instance_extensions = malloc(*count * sizeof(char *)); - #ifdef DEBUG log_info("Required Vulkan extensions: "); - #endif for (usize i = 0; i < rgfw_extension_count; i++) { - #ifdef DEBUG printf("%s ", rgfw_extensions[i]); - #endif instance_extensions[i] = rgfw_extensions[i]; } for (usize i = 0; i < extension_count; i++) { - #ifdef DEBUG printf("%s ", extensions[i]); - #endif instance_extensions[rgfw_extension_count + i] = extensions[i]; } - #ifdef DEBUG printf("\n"); - #endif return instance_extensions; } @@ -92,8 +85,10 @@ void vk_instance_init(struct renderer_context *context) VkResult result = vkCreateInstance(&instance_info, NULL, &context->instance); switch(result) { case VK_ERROR_INCOMPATIBLE_DRIVER: + free(extension_names); fatal("Incompatible driver.\n"); case VK_ERROR_LAYER_NOT_PRESENT: + free(extension_names); fatal("Requested vulkan layers are not present.\n"); case VK_SUCCESS: log_info("Vulkan instance created.\n"); @@ -104,3 +99,8 @@ void vk_instance_init(struct renderer_context *context) free(extension_names); } + +void vk_instance_deinit(struct renderer_context *context) +{ + vkDestroyInstance(context->instance, NULL); +} diff --git a/vk/instance.h b/vk/instance.h index 729c778..bc41ec4 100644 --- a/vk/instance.h +++ b/vk/instance.h @@ -1,8 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause #ifndef INSTANCE_H #define INSTANCE_H #include "../rendering/renderer.h" void vk_instance_init(struct renderer_context *context); +void vk_instance_deinit(struct renderer_context *context); #endif diff --git a/vk/physical_device.c b/vk/physical_device.c index e69de29..ab4e5d2 100644 --- a/vk/physical_device.c +++ b/vk/physical_device.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: BSD-3-Clause +#include "physical_device.h" +#include "../core/log.h" +#include +#include + +void vk_physical_device_pick(struct renderer_context *context) +{ + u32 device_count = 0; + + vkEnumeratePhysicalDevices(context->instance, &device_count, NULL); + + VkPhysicalDevice *devices = malloc(sizeof(VkPhysicalDevice) * device_count); + + if (vkEnumeratePhysicalDevices(context->instance, &device_count, devices) != VK_SUCCESS || device_count == 0) { + fatal("Can't get physical devices.\n"); + } + + log_info("Available devices:\n"); + for (usize i = 0; i < device_count; i++) { + VkPhysicalDeviceProperties properties; + vkGetPhysicalDeviceProperties(devices[i], &properties); + printf("%s\n", properties.deviceName); + } + + context->physical_device = devices[0]; + + free(devices); +} + +struct vector *vk_physical_device_get_extensions(struct renderer_context *context) +{ + u32 property_count = 0; + VkExtensionProperties *properties = NULL; + + vkEnumerateDeviceExtensionProperties(context->physical_device, NULL, &property_count, NULL); + + struct vector *extensions = vector_init(property_count, sizeof(char *)); + properties = (VkExtensionProperties *) malloc(sizeof(VkExtensionProperties) * property_count); + + if (vkEnumerateDeviceExtensionProperties(context->physical_device, NULL, &property_count, properties) != VK_SUCCESS) { + free(properties); + fatal("Can't get physical device extension properties.\n"); + } + + for (usize i = 0; i < property_count; i++) { + char *name = malloc(strlen(properties[i].extensionName) + 1); + strcpy(name, properties[i].extensionName); + vector_push(extensions, char *, name); + } + + free(properties); + + return extensions; +} + +void vk_physical_device_select_family_indices(struct renderer_context *context) +{ + u32 property_count = 0; + VkQueueFamilyProperties *properties = NULL; + + vkGetPhysicalDeviceQueueFamilyProperties(context->physical_device, &property_count, NULL); + + properties = (VkQueueFamilyProperties *) malloc(sizeof(VkExtensionProperties) * property_count); + + vkGetPhysicalDeviceQueueFamilyProperties(context->physical_device, &property_count, properties); + + for (u32 i = 0; i < property_count; i++) { + VkQueueFamilyProperties prop = properties[i]; + + if (prop.queueFlags & VK_QUEUE_GRAPHICS_BIT) { + context->queue_indices.graphics = i; + } + } + + free(properties); +} diff --git a/vk/physical_device.h b/vk/physical_device.h new file mode 100644 index 0000000..27d399d --- /dev/null +++ b/vk/physical_device.h @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: BSD-3-Clause +#ifndef PHYSICAL_DEVICE_H +#define PHYSICAL_DEVICE_H + +#include "vk.h" +#include "../types.h" +#include "../core/vector.h" + + +/* + * Get the list of all available devices and + * pick the best option. + */ +void vk_physical_device_pick(struct renderer_context *context); +/* + * Get the list of all available device + * extensions and return a vector containing + * those. + */ +struct vector *vk_physical_device_get_extensions(struct renderer_context *context); +/* + * The physical device is responsible of selecting + * the queue family indices, used later by the + * device to create the queues. This function + * sets the family indices in the renderer context. + */ +void vk_physical_device_select_family_indices(struct renderer_context *context); + +#endif diff --git a/vk/platform.c b/vk/platform.c index de73672..c9b3196 100644 --- a/vk/platform.c +++ b/vk/platform.c @@ -36,6 +36,7 @@ int platform_run(i32 argc, u8 **argv) while (RGFW_window_checkEvent(win, &event)); } + renderer_context_deinit(context); RGFW_window_close(win); return 0; } diff --git a/vk/renderer.c b/vk/renderer.c index 8381d3c..65aa1ad 100644 --- a/vk/renderer.c +++ b/vk/renderer.c @@ -1,18 +1,30 @@ // SPDX-License-Identifier: BSD-3-Clause #include "../rendering/renderer.h" -#include #include "instance.h" +#include "physical_device.h" +#include "device.h" #include "vk.h" +#include "../core/arena.h" #include +#include struct renderer_context *renderer_context_init(void) { - struct renderer_context *context = (struct renderer_context *) malloc(sizeof(struct renderer_context)); + struct renderer_context *context = (struct renderer_context *) arena_alloc(global_arena, (sizeof(struct renderer_context))); vk_instance_init(context); + vk_physical_device_pick(context); + vk_physical_device_select_family_indices(context); + vk_device_init(context); return context; } +void renderer_context_deinit(struct renderer_context *context) +{ + vk_device_deinit(context); + vk_instance_deinit(context); +} + struct mesh *renderer_build_chunk_mesh(void) { return NULL; diff --git a/vk/vk.h b/vk/vk.h index 5454737..447acbf 100644 --- a/vk/vk.h +++ b/vk/vk.h @@ -1,10 +1,20 @@ +// SPDX-License-Identifier: BSD-3-Clause #ifndef VK_H #define VK_H #include +#include "../types.h" + +struct queue_family_indices { + u32 graphics, present; +}; + struct renderer_context { VkInstance instance; VkPhysicalDevice physical_device; + VkDevice device; + VkQueue graphics_queue; + struct queue_family_indices queue_indices; }; #endif diff --git a/world/block.h b/world/block.h index 99c0af2..5118e29 100644 --- a/world/block.h +++ b/world/block.h @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #ifdef BLOCK_H #define BLOCK_H diff --git a/world/chunk.h b/world/chunk.h index a1df8f4..6dde780 100644 --- a/world/chunk.h +++ b/world/chunk.h @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: BSD-3-Clause #ifndef CHUNK_H #define CHUNK_H