feat(layout)!: move /gl and /vk into rendering directory
as I was adding the audio module I realized it looks really ugly that the audio-related stuff will be in its own directory and the renderer modules will be scattered in root directory it makes way more sense this way Signed-off-by: Lorenzo Torres <lorenzo@sagittarius-a.org>
This commit is contained in:
parent
5a4db88436
commit
1070d03815
14 changed files with 6187 additions and 8 deletions
60
rendering/vk/device.c
Normal file
60
rendering/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
rendering/vk/device.h
Normal file
10
rendering/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
|
||||
104
rendering/vk/instance.c
Normal file
104
rendering/vk/instance.c
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
#include "instance.h"
|
||||
#include "../../core/log.h"
|
||||
#define RGFW_VULKAN
|
||||
#include "../../rgfw.h"
|
||||
#include "vk.h"
|
||||
|
||||
const char *extensions[] = {
|
||||
#ifdef PLATFORM_MACOS
|
||||
VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME,
|
||||
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
|
||||
#endif
|
||||
NULL
|
||||
};
|
||||
|
||||
#ifdef DEBUG
|
||||
const char *layers[] = {
|
||||
"VK_LAYER_KHRONOS_validation"
|
||||
};
|
||||
#define LAYER_COUNT (sizeof(layers)/sizeof(layers[0]))
|
||||
#else
|
||||
#define LAYER_COUNT 0
|
||||
const char *layers[] = {NULL};
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Vulkan is modular by design. This means that some features that might not be
|
||||
* always needed are not enabled by default. To enable those features (like
|
||||
* macOS platform compatibility or windowing) extensions are needed. This
|
||||
* function checks which extensions are required by RGFW and adds extensions
|
||||
* required by the game itself.
|
||||
*/
|
||||
const char **vk_instance_extensions(usize * count)
|
||||
{
|
||||
usize rgfw_extension_count = 0;
|
||||
const char **rgfw_extensions = RGFW_getRequiredInstanceExtensions_Vulkan(&rgfw_extension_count);
|
||||
|
||||
usize extension_count = sizeof(extensions) / sizeof(extensions[0]) - 1;
|
||||
*count = rgfw_extension_count + extension_count;
|
||||
const char **instance_extensions = malloc(*count * sizeof(char *));
|
||||
|
||||
log_info("Required Vulkan extensions: ");
|
||||
|
||||
for (usize i = 0; i < rgfw_extension_count; i++) {
|
||||
printf("%s ", rgfw_extensions[i]);
|
||||
instance_extensions[i] = rgfw_extensions[i];
|
||||
}
|
||||
|
||||
for (usize i = 0; i < extension_count; i++) {
|
||||
printf("%s ", extensions[i]);
|
||||
instance_extensions[rgfw_extension_count + i] = extensions[i];
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
return instance_extensions;
|
||||
}
|
||||
|
||||
void vk_instance_init(struct renderer_context *context)
|
||||
{
|
||||
VkApplicationInfo application_info = {
|
||||
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||
.pApplicationName = "topaz",
|
||||
.applicationVersion = 0,
|
||||
.pEngineName = "topaz",
|
||||
.engineVersion = 0,
|
||||
.apiVersion = VK_API_VERSION_1_4
|
||||
};
|
||||
|
||||
usize extension_count = 0;
|
||||
const char **extension_names = vk_instance_extensions(&extension_count);
|
||||
|
||||
VkInstanceCreateInfo instance_info = {
|
||||
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||
.pApplicationInfo = &application_info,
|
||||
.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
|
||||
.enabledLayerCount = LAYER_COUNT,
|
||||
.ppEnabledLayerNames = layers,
|
||||
.enabledExtensionCount = extension_count,
|
||||
.ppEnabledExtensionNames = extension_names,
|
||||
};
|
||||
|
||||
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");
|
||||
break;
|
||||
default:
|
||||
fatal("Can't create Vulkan instance.\n");
|
||||
}
|
||||
|
||||
free(extension_names);
|
||||
}
|
||||
|
||||
void vk_instance_deinit(struct renderer_context *context)
|
||||
{
|
||||
vkDestroyInstance(context->instance, NULL);
|
||||
}
|
||||
10
rendering/vk/instance.h
Normal file
10
rendering/vk/instance.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
#ifndef INSTANCE_H
|
||||
#define INSTANCE_H
|
||||
|
||||
#include "../renderer.h"
|
||||
|
||||
void vk_instance_init(struct renderer_context *context);
|
||||
void vk_instance_deinit(struct renderer_context *context);
|
||||
|
||||
#endif
|
||||
77
rendering/vk/physical_device.c
Normal file
77
rendering/vk/physical_device.c
Normal 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);
|
||||
}
|
||||
26
rendering/vk/physical_device.h
Normal file
26
rendering/vk/physical_device.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* 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
|
||||
41
rendering/vk/platform.c
Normal file
41
rendering/vk/platform.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
|
||||
#define RGFW_VULKAN
|
||||
#define RGFW_IMPLEMENTATION
|
||||
#include "../../rgfw.h"
|
||||
#include <vulkan/vulkan.h>
|
||||
#include "../../core/log.h"
|
||||
|
||||
#ifdef PLATFORM_MACOS
|
||||
#include <vulkan/vulkan_macos.h>
|
||||
#include <vulkan/vulkan_metal.h>
|
||||
#endif
|
||||
|
||||
#include "../renderer.h"
|
||||
|
||||
/*
|
||||
* This function is the entrypoint for the whole game. Its role is to
|
||||
* initialize Vulkan, create the renderer and start the game loop.
|
||||
*/
|
||||
int platform_run(i32 argc, u8 * *argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
log_info("Using Vulkan as rendering backend.\n");
|
||||
|
||||
RGFW_window *win = RGFW_createWindow("topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
|
||||
RGFW_window_show(win);
|
||||
RGFW_window_setExitKey(win, RGFW_escape);
|
||||
|
||||
struct renderer_context *context = renderer_context_init();
|
||||
|
||||
while (RGFW_window_shouldClose(win) == RGFW_FALSE) {
|
||||
RGFW_event event;
|
||||
while (RGFW_window_checkEvent(win, &event));
|
||||
}
|
||||
|
||||
renderer_context_deinit(context);
|
||||
RGFW_window_close(win);
|
||||
return 0;
|
||||
}
|
||||
43
rendering/vk/renderer.c
Normal file
43
rendering/vk/renderer.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||
#include "../renderer.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 *)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;
|
||||
}
|
||||
|
||||
void renderer_draw_mesh(struct renderer_context *context, struct mesh mesh)
|
||||
{
|
||||
(void) context;
|
||||
(void)mesh;
|
||||
}
|
||||
|
||||
void renderer_draw_chunk(struct renderer_context *context, struct mesh mesh)
|
||||
{
|
||||
(void)mesh;
|
||||
(void)context;
|
||||
}
|
||||
20
rendering/vk/vk.h
Normal file
20
rendering/vk/vk.h
Normal file
|
|
@ -0,0 +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