implemented Vulkan surface creation
This commit is contained in:
parent
fa2a4887df
commit
b6b0c5091b
13 changed files with 128 additions and 18 deletions
8
makefile
8
makefile
|
|
@ -21,8 +21,14 @@ ifeq (${GRAPHICS_BACKEND},vk)
|
||||||
rendering/vk/renderer.c\
|
rendering/vk/renderer.c\
|
||||||
rendering/vk/instance.c\
|
rendering/vk/instance.c\
|
||||||
rendering/vk/physical_device.c\
|
rendering/vk/physical_device.c\
|
||||||
rendering/vk/device.c
|
rendering/vk/device.c\
|
||||||
|
rendering/vk/surface.c
|
||||||
|
|
||||||
|
ifeq (${PLATFORM},Darwin)
|
||||||
|
SRC += rendering/vk/macos_platform.c
|
||||||
endif
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
OBJ:=${SRC:.c=.o}
|
OBJ:=${SRC:.c=.o}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,10 @@
|
||||||
#ifndef RENDERER_H
|
#ifndef RENDERER_H
|
||||||
#define RENDERER_H
|
#define RENDERER_H
|
||||||
#include "../types.h"
|
#include "../types.h"
|
||||||
|
#ifdef BACKEND_VK
|
||||||
|
#define RGFW_VULKAN
|
||||||
|
#endif
|
||||||
|
#include "../rgfw.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A mesh is a drawable object represented as an index (offset) in the global
|
* A mesh is a drawable object represented as an index (offset) in the global
|
||||||
|
|
@ -20,7 +24,7 @@ struct mesh {
|
||||||
*/
|
*/
|
||||||
struct renderer_context;
|
struct renderer_context;
|
||||||
|
|
||||||
struct renderer_context *renderer_context_init(void);
|
struct renderer_context *renderer_context_init(RGFW_window *window);
|
||||||
void renderer_context_deinit(struct renderer_context *context);
|
void renderer_context_deinit(struct renderer_context *context);
|
||||||
struct mesh *renderer_build_chunk_mesh(void);
|
struct mesh *renderer_build_chunk_mesh(void);
|
||||||
void renderer_draw_mesh(struct renderer_context *context, struct mesh mesh);
|
void renderer_draw_mesh(struct renderer_context *context, struct mesh mesh);
|
||||||
|
|
|
||||||
|
|
@ -24,19 +24,27 @@ void vk_device_init(struct renderer_context *context)
|
||||||
}
|
}
|
||||||
|
|
||||||
f32 priority = 0.0;
|
f32 priority = 0.0;
|
||||||
VkDeviceQueueCreateInfo graphics_queue_create_info = {
|
VkDeviceQueueCreateInfo graphics_queue_create_info[2];
|
||||||
|
graphics_queue_create_info[0] = (VkDeviceQueueCreateInfo){
|
||||||
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
||||||
.queueFamilyIndex = context->queue_indices.graphics,
|
.queueFamilyIndex = context->queue_indices.graphics,
|
||||||
.queueCount = 1,
|
.queueCount = 1,
|
||||||
.pQueuePriorities = &priority
|
.pQueuePriorities = &priority
|
||||||
};
|
};
|
||||||
|
|
||||||
|
graphics_queue_create_info[1] = (VkDeviceQueueCreateInfo){
|
||||||
|
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
||||||
|
.queueFamilyIndex = context->queue_indices.present,
|
||||||
|
.queueCount = 1,
|
||||||
|
.pQueuePriorities = &priority
|
||||||
|
};
|
||||||
|
|
||||||
VkDeviceCreateInfo device_create_info = {
|
VkDeviceCreateInfo device_create_info = {
|
||||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||||
.pNext = NULL,
|
.pNext = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.queueCreateInfoCount = 1,
|
.queueCreateInfoCount = 2,
|
||||||
.pQueueCreateInfos = &graphics_queue_create_info,
|
.pQueueCreateInfos = graphics_queue_create_info,
|
||||||
.enabledLayerCount = 0,
|
.enabledLayerCount = 0,
|
||||||
.ppEnabledLayerNames = NULL,
|
.ppEnabledLayerNames = NULL,
|
||||||
.enabledExtensionCount = device_extensions->length,
|
.enabledExtensionCount = device_extensions->length,
|
||||||
|
|
@ -50,7 +58,8 @@ void vk_device_init(struct renderer_context *context)
|
||||||
fatal("Can't create Vulkan device.\n");
|
fatal("Can't create Vulkan device.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
vkGetDeviceQueue(context->device, graphics_queue_create_info.queueFamilyIndex, 0, &context->graphics_queue);
|
vkGetDeviceQueue(context->device, graphics_queue_create_info[0].queueFamilyIndex, 0, &context->graphics_queue);
|
||||||
|
vkGetDeviceQueue(context->device, graphics_queue_create_info[1].queueFamilyIndex, 0, &context->present_queue);
|
||||||
|
|
||||||
vector_deinit(physical_device_extensions);
|
vector_deinit(physical_device_extensions);
|
||||||
vector_deinit(device_extensions);
|
vector_deinit(device_extensions);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
/* SPDX-License-Identifier:BSD-3-Clause */
|
/* SPDX-License-Identifier:BSD-3-Clause */
|
||||||
#include "instance.h"
|
#include "instance.h"
|
||||||
#include "../../core/log.h"
|
#include "../../core/log.h"
|
||||||
#define RGFW_VULKAN
|
|
||||||
#include "../../rgfw.h"
|
|
||||||
#include "vk.h"
|
#include "vk.h"
|
||||||
|
|
||||||
const char *extensions[] = {
|
const char *extensions[] = {
|
||||||
|
|
|
||||||
24
rendering/vk/macos_platform.c
Normal file
24
rendering/vk/macos_platform.c
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef MACOS_PLATFORM
|
||||||
|
#define MACOS_PLATFORM
|
||||||
|
|
||||||
|
#include <objc/runtime.h>
|
||||||
|
#include <objc/message.h>
|
||||||
|
#include "../../rgfw.h"
|
||||||
|
|
||||||
|
#define cls objc_getClass
|
||||||
|
#define sel sel_getUid
|
||||||
|
typedef id (*object_message_send)(id, SEL, id);
|
||||||
|
typedef id (*class_message_send)(Class, SEL, ...);
|
||||||
|
|
||||||
|
#define msg ((object_message_send)objc_msgSend)
|
||||||
|
#define cls_msg ((class_message_send)objc_msgSend)
|
||||||
|
|
||||||
|
void macos_set_window_layer(RGFW_window *window)
|
||||||
|
{
|
||||||
|
Class CAMetalLayer = cls("CAMetalLayer");
|
||||||
|
id metal_layer = cls_msg(CAMetalLayer, sel("layer"));
|
||||||
|
id view = (id)RGFW_window_getView_OSX(window);
|
||||||
|
msg(view, sel("setLayer:"), metal_layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -54,6 +54,7 @@ struct vector *vk_physical_device_get_extensions(struct renderer_context *contex
|
||||||
return extensions;
|
return extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void vk_physical_device_select_family_indices(struct renderer_context *context)
|
void vk_physical_device_select_family_indices(struct renderer_context *context)
|
||||||
{
|
{
|
||||||
u32 property_count = 0;
|
u32 property_count = 0;
|
||||||
|
|
@ -64,13 +65,34 @@ void vk_physical_device_select_family_indices(struct renderer_context *context)
|
||||||
properties = (VkQueueFamilyProperties *) malloc(sizeof(VkExtensionProperties) * property_count);
|
properties = (VkQueueFamilyProperties *) malloc(sizeof(VkExtensionProperties) * property_count);
|
||||||
|
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(context->physical_device, &property_count, properties);
|
vkGetPhysicalDeviceQueueFamilyProperties(context->physical_device, &property_count, properties);
|
||||||
|
/*
|
||||||
|
* Use 0xaa as a known value to specify that these
|
||||||
|
* indices have not been found yet.
|
||||||
|
*/
|
||||||
|
context->queue_indices.graphics = 0xaa;
|
||||||
|
context->queue_indices.present = 0xaa;
|
||||||
|
/*
|
||||||
|
* This loop iterates over all the family properties
|
||||||
|
* provided by the physical device and searches for
|
||||||
|
* two unique queue family indices, one for presentation
|
||||||
|
* and one for graphics.
|
||||||
|
*/
|
||||||
for (u32 i = 0; i < property_count; i++) {
|
for (u32 i = 0; i < property_count; i++) {
|
||||||
|
if (context->queue_indices.graphics != 0xaa && context->queue_indices.present != 0xaa) break;
|
||||||
VkQueueFamilyProperties prop = properties[i];
|
VkQueueFamilyProperties prop = properties[i];
|
||||||
|
|
||||||
if (prop.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
if ((prop.queueFlags & VK_QUEUE_GRAPHICS_BIT) && context->queue_indices.graphics == 0xaa) {
|
||||||
context->queue_indices.graphics = i;
|
context->queue_indices.graphics = i;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VkBool32 present_support = VK_FALSE;
|
||||||
|
if (vkGetPhysicalDeviceSurfaceSupportKHR(context->physical_device, i, context->surface, &present_support) != VK_SUCCESS) {
|
||||||
|
free(properties);
|
||||||
|
fatal("Can't check for surface support.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (present_support == VK_TRUE) context->queue_indices.present = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(properties);
|
free(properties);
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,17 @@
|
||||||
|
|
||||||
#define RGFW_VULKAN
|
#define RGFW_VULKAN
|
||||||
#define RGFW_IMPLEMENTATION
|
#define RGFW_IMPLEMENTATION
|
||||||
#include "../../rgfw.h"
|
|
||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
#include "../../core/log.h"
|
#include "../../core/log.h"
|
||||||
|
|
||||||
|
#include "../renderer.h"
|
||||||
|
|
||||||
#ifdef PLATFORM_MACOS
|
#ifdef PLATFORM_MACOS
|
||||||
#include <vulkan/vulkan_macos.h>
|
#include <vulkan/vulkan_macos.h>
|
||||||
#include <vulkan/vulkan_metal.h>
|
#include <vulkan/vulkan_metal.h>
|
||||||
|
void macos_set_window_layer(RGFW_window *window);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "../renderer.h"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function is the entrypoint for the whole game. Its role is to
|
* This function is the entrypoint for the whole game. Its role is to
|
||||||
* initialize Vulkan, create the renderer and start the game loop.
|
* initialize Vulkan, create the renderer and start the game loop.
|
||||||
|
|
@ -27,8 +27,9 @@ int platform_run(i32 argc, u8 * *argv)
|
||||||
RGFW_window *win = RGFW_createWindow("topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
|
RGFW_window *win = RGFW_createWindow("topaz", 0, 0, 800, 600, RGFW_windowCenter | RGFW_windowNoResize | RGFW_windowHide);
|
||||||
RGFW_window_show(win);
|
RGFW_window_show(win);
|
||||||
RGFW_window_setExitKey(win, RGFW_escape);
|
RGFW_window_setExitKey(win, RGFW_escape);
|
||||||
|
macos_set_window_layer(win);
|
||||||
|
|
||||||
struct renderer_context *context = renderer_context_init();
|
struct renderer_context *context = renderer_context_init(win);
|
||||||
|
|
||||||
while (RGFW_window_shouldClose(win) == RGFW_FALSE) {
|
while (RGFW_window_shouldClose(win) == RGFW_FALSE) {
|
||||||
RGFW_event event;
|
RGFW_event event;
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,19 @@
|
||||||
#include "instance.h"
|
#include "instance.h"
|
||||||
#include "physical_device.h"
|
#include "physical_device.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
|
#include "surface.h"
|
||||||
#include "vk.h"
|
#include "vk.h"
|
||||||
#include "../../core/arena.h"
|
#include "../../core/arena.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
struct renderer_context *renderer_context_init(void)
|
struct renderer_context *renderer_context_init(RGFW_window *window)
|
||||||
{
|
{
|
||||||
struct renderer_context *context = (struct renderer_context *)arena_alloc(global_arena, (sizeof(struct renderer_context)));
|
struct renderer_context *context = (struct renderer_context *)arena_alloc(global_arena, (sizeof(struct renderer_context)));
|
||||||
|
context->window = window;
|
||||||
|
|
||||||
vk_instance_init(context);
|
vk_instance_init(context);
|
||||||
|
vk_surface_init(context);
|
||||||
vk_physical_device_pick(context);
|
vk_physical_device_pick(context);
|
||||||
vk_physical_device_select_family_indices(context);
|
vk_physical_device_select_family_indices(context);
|
||||||
vk_device_init(context);
|
vk_device_init(context);
|
||||||
|
|
@ -21,6 +24,7 @@ struct renderer_context *renderer_context_init(void)
|
||||||
|
|
||||||
void renderer_context_deinit(struct renderer_context *context)
|
void renderer_context_deinit(struct renderer_context *context)
|
||||||
{
|
{
|
||||||
|
vk_surface_deinit(context);
|
||||||
vk_device_deinit(context);
|
vk_device_deinit(context);
|
||||||
vk_instance_deinit(context);
|
vk_instance_deinit(context);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
23
rendering/vk/surface.c
Normal file
23
rendering/vk/surface.c
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include "surface.h"
|
||||||
|
#include "../../core/log.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This function is defined in rgfw.h
|
||||||
|
*/
|
||||||
|
VkResult RGFW_window_createSurface_Vulkan(RGFW_window * win, VkInstance instance, VkSurfaceKHR * surface);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RGFW exposes a platform independent function to
|
||||||
|
* create a Vulkan surface.
|
||||||
|
*/
|
||||||
|
void vk_surface_init(struct renderer_context *context)
|
||||||
|
{
|
||||||
|
if (RGFW_window_createSurface_Vulkan(context->window, context->instance, &context->surface) != VK_SUCCESS) {
|
||||||
|
fatal("Can't create Vulkan surface.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void vk_surface_deinit(struct renderer_context *context)
|
||||||
|
{
|
||||||
|
vkDestroySurfaceKHR(context->instance, context->surface, NULL);
|
||||||
|
}
|
||||||
9
rendering/vk/surface.h
Normal file
9
rendering/vk/surface.h
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef SURFACE_H
|
||||||
|
#define SURFACE_H
|
||||||
|
|
||||||
|
#include "vk.h"
|
||||||
|
|
||||||
|
void vk_surface_init(struct renderer_context *context);
|
||||||
|
void vk_surface_deinit(struct renderer_context *context);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
#include "../../types.h"
|
#include "../../types.h"
|
||||||
|
#include "../../rgfw.h"
|
||||||
|
|
||||||
struct queue_family_indices {
|
struct queue_family_indices {
|
||||||
u32 graphics, present;
|
u32 graphics, present;
|
||||||
|
|
@ -13,8 +14,10 @@ struct renderer_context {
|
||||||
VkInstance instance;
|
VkInstance instance;
|
||||||
VkPhysicalDevice physical_device;
|
VkPhysicalDevice physical_device;
|
||||||
VkDevice device;
|
VkDevice device;
|
||||||
VkQueue graphics_queue;
|
VkQueue graphics_queue, present_queue;
|
||||||
|
VkSurfaceKHR surface;
|
||||||
struct queue_family_indices queue_indices;
|
struct queue_family_indices queue_indices;
|
||||||
|
RGFW_window *window;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
10
rgfw.h
10
rgfw.h
|
|
@ -1554,7 +1554,7 @@ extern "C" {
|
||||||
* @param win a pointer to the target window
|
* @param win a pointer to the target window
|
||||||
* @return A pointer to the macOS view object, or NULL if not on macOS
|
* @return A pointer to the macOS view object, or NULL if not on macOS
|
||||||
*/
|
*/
|
||||||
RGFWDEF void *RGFW_window_getView_OSX(RGFW_window * win);
|
void *RGFW_window_getView_OSX(RGFW_window * win);
|
||||||
|
|
||||||
/**!
|
/**!
|
||||||
* @brief retrieves the macOS window object
|
* @brief retrieves the macOS window object
|
||||||
|
|
@ -5071,6 +5071,14 @@ const char *extensions = ((const char *(*) (u32))RGFW_glGetString) (RGFW_GL_EXTE
|
||||||
|
|
||||||
return vkCreateWin32SurfaceKHR(instance, &win32, NULL, surface);
|
return vkCreateWin32SurfaceKHR(instance, &win32, NULL, surface);
|
||||||
#elif defined(RGFW_MACOS) && !defined(RGFW_MACOS_X11)
|
#elif defined(RGFW_MACOS) && !defined(RGFW_MACOS_X11)
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wunused-local-typedef"
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
||||||
|
#include <vulkan/vulkan_macos.h>
|
||||||
|
#include <vulkan/vulkan_metal.h>
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#pragma clang diagnostic pop
|
||||||
void *contentView = ((void *(*) (id, SEL))objc_msgSend) ((id) win->src.window, sel_getUid("contentView"));
|
void *contentView = ((void *(*) (id, SEL))objc_msgSend) ((id) win->src.window, sel_getUid("contentView"));
|
||||||
VkMacOSSurfaceCreateInfoMVK macos = {VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK, 0, 0, (void *)contentView};
|
VkMacOSSurfaceCreateInfoMVK macos = {VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK, 0, 0, (void *)contentView};
|
||||||
return vkCreateMacOSSurfaceMVK(instance, &macos, NULL, surface);
|
return vkCreateMacOSSurfaceMVK(instance, &macos, NULL, surface);
|
||||||
|
|
|
||||||
1
types.h
1
types.h
|
|
@ -20,6 +20,5 @@ typedef size_t usize;
|
||||||
typedef float f32;
|
typedef float f32;
|
||||||
typedef double f64;
|
typedef double f64;
|
||||||
|
|
||||||
typedef uint8_t bool;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue