added vulkan device and queues creation
This commit is contained in:
parent
89edee7249
commit
6c31a3ce27
20 changed files with 253 additions and 18 deletions
60
vk/device.c
Normal file
60
vk/device.c
Normal 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
10
vk/device.h
Normal 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
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
29
vk/physical_device.h
Normal 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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
10
vk/vk.h
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue