added vulkan device and queues creation

This commit is contained in:
Lorenzo Torres 2025-11-03 13:08:46 +01:00
parent 89edee7249
commit 6c31a3ce27
20 changed files with 253 additions and 18 deletions

View file

@ -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

View file

@ -2,6 +2,7 @@
#include "arena.h"
#include <stdlib.h>
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;
}

View file

@ -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);

View file

@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#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

View file

@ -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;

View file

@ -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 $@

View file

@ -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);

13
topaz.c
View file

@ -1,10 +1,17 @@
// SPDX-License-Identifier: BSD-3-Clause
#include <stdio.h>
#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;
}

View file

@ -20,4 +20,6 @@ typedef size_t usize;
typedef float f32;
typedef double f64;
typedef uint8_t bool;
#endif

60
vk/device.c Normal file
View file

@ -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 <string.h>
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);
}

10
vk/device.h Normal file
View file

@ -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

View file

@ -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);
}

View file

@ -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

View file

@ -0,0 +1,77 @@
// SPDX-License-Identifier: BSD-3-Clause
#include "physical_device.h"
#include "../core/log.h"
#include <stdio.h>
#include <string.h>
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);
}

29
vk/physical_device.h Normal file
View file

@ -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

View file

@ -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;
}

View file

@ -1,18 +1,30 @@
// SPDX-License-Identifier: BSD-3-Clause
#include "../rendering/renderer.h"
#include <vulkan/vulkan.h>
#include "instance.h"
#include "physical_device.h"
#include "device.h"
#include "vk.h"
#include "../core/arena.h"
#include <stdlib.h>
#include <stdio.h>
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;

10
vk/vk.h
View file

@ -1,10 +1,20 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef VK_H
#define VK_H
#include <vulkan/vulkan.h>
#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

View file

@ -1,3 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifdef BLOCK_H
#define BLOCK_H

View file

@ -1,3 +1,4 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef CHUNK_H
#define CHUNK_H