implemented vulkan instance creation
This commit is contained in:
parent
84ee267b5d
commit
89edee7249
11 changed files with 208 additions and 10 deletions
106
vk/instance.c
Normal file
106
vk/instance.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#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
|
||||
};
|
||||
|
||||
#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]);
|
||||
*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;
|
||||
}
|
||||
|
||||
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:
|
||||
fatal("Incompatible driver.\n");
|
||||
case VK_ERROR_LAYER_NOT_PRESENT:
|
||||
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);
|
||||
}
|
||||
8
vk/instance.h
Normal file
8
vk/instance.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef INSTANCE_H
|
||||
#define INSTANCE_H
|
||||
|
||||
#include "../rendering/renderer.h"
|
||||
|
||||
void vk_instance_init(struct renderer_context *context);
|
||||
|
||||
#endif
|
||||
0
vk/physical_device.c
Normal file
0
vk/physical_device.c
Normal file
41
vk/platform.c
Normal file
41
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 "../rendering/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));
|
||||
}
|
||||
|
||||
RGFW_window_close(win);
|
||||
return 0;
|
||||
}
|
||||
29
vk/renderer.c
Normal file
29
vk/renderer.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
#include "../rendering/renderer.h"
|
||||
#include <vulkan/vulkan.h>
|
||||
#include "instance.h"
|
||||
#include "vk.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct renderer_context *renderer_context_init(void)
|
||||
{
|
||||
struct renderer_context *context = (struct renderer_context *) malloc(sizeof(struct renderer_context));
|
||||
|
||||
vk_instance_init(context);
|
||||
return context;
|
||||
}
|
||||
|
||||
struct mesh *renderer_build_chunk_mesh(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void renderer_draw_mesh(struct mesh mesh)
|
||||
{
|
||||
(void) mesh;
|
||||
}
|
||||
|
||||
void renderer_draw_chunk(struct mesh mesh)
|
||||
{
|
||||
(void) mesh;
|
||||
}
|
||||
10
vk/vk.h
Normal file
10
vk/vk.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef VK_H
|
||||
#define VK_H
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
struct renderer_context {
|
||||
VkInstance instance;
|
||||
VkPhysicalDevice physical_device;
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue