Compare commits

...

2 commits

Author SHA1 Message Date
65d6c42a47
go back to global vars
I dislike this strongly but it's better to keep them in init.c I think
this is all internals either way right now I just wanna clean things up
so that introducing different backends isn't tedious
2024-09-24 10:36:29 +02:00
402e45c766
almost improve physical dev module
draw function needs some help
2024-09-22 20:23:10 +02:00
9 changed files with 35 additions and 34 deletions

View file

@ -22,8 +22,6 @@
#include <stdlib.h>
#include <string.h>
static GLFWwindow *m_window = NULL;
static void framebuffer_resize_callback(GLFWwindow *window, int width, int height) {
(void)window; (void)width; (void)height;
g_framebuffer_resized = true;
@ -112,16 +110,16 @@ bool ptk_init(const size_t width, const size_t height, const char *title, const
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
m_window = glfwCreateWindow(width, height, title, NULL, NULL);
g_window = glfwCreateWindow(width, height, title, NULL, NULL);
if (m_window == NULL) {
if (g_window == NULL) {
PTK_ERR("failed creating GLFW window");
return false;
}
glfwSetFramebufferSizeCallback(m_window, framebuffer_resize_callback);
glfwSetKeyCallback(m_window, key_callback);
glfwSetMouseButtonCallback(m_window, mouse_button_callback);
glfwSetFramebufferSizeCallback(g_window, framebuffer_resize_callback);
glfwSetKeyCallback(g_window, key_callback);
glfwSetMouseButtonCallback(g_window, mouse_button_callback);
vk_init_vertices();
@ -133,9 +131,9 @@ bool ptk_init(const size_t width, const size_t height, const char *title, const
// this fixes an issue where components would get drawn squished
// and their hitboxes wouldn't match up with them visually
int actual_width = 0, actual_height = 0;
glfwGetFramebufferSize(m_window, &actual_width, &actual_height);
glfwGetFramebufferSize(g_window, &actual_width, &actual_height);
if (!vk_init(m_window, actual_width, actual_height, title, application_version)) {
if (!vk_init(g_window, actual_width, actual_height, title, application_version)) {
PTK_ERR("failed initializing vulkan");
return false;
}
@ -195,9 +193,9 @@ PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) {
int ptk_run(PtkHandle root) {
vk_init_components(root);
while (!glfwWindowShouldClose(m_window)) {
while (!glfwWindowShouldClose(g_window)) {
glfwPollEvents();
if (!vk_draw_frame(m_window, g_surface)) {
if (!vk_draw_frame()) {
break;
}
}

View file

@ -14,7 +14,7 @@
bool g_framebuffer_resized = false;
uint32_t g_current_frame = 0;
bool vk_draw_frame(GLFWwindow *window, VkSurfaceKHR surface) {
bool vk_draw_frame(void) {
vkWaitForFences(g_dev, 1, &g_in_flight_fences.data[g_current_frame], VK_TRUE, UINT64_MAX);
uint32_t image_index;
@ -28,7 +28,7 @@ bool vk_draw_frame(GLFWwindow *window, VkSurfaceKHR surface) {
);
if (acquire_next_image_result == VK_ERROR_OUT_OF_DATE_KHR) {
if (!vk_recreate_swapchain(window, surface)) {
if (!vk_recreate_swapchain(g_window, g_physical_dev, g_surface)) {
return false;
}
return true;
@ -85,7 +85,7 @@ bool vk_draw_frame(GLFWwindow *window, VkSurfaceKHR surface) {
g_framebuffer_resized
) {
g_framebuffer_resized = false;
if (!vk_recreate_swapchain(window, surface)) {
if (!vk_recreate_swapchain(g_window, g_physical_dev, g_surface)) {
return false;
}
PTK_TRACE("recreated swapchain");

View file

@ -14,6 +14,6 @@
extern bool g_framebuffer_resized;
extern uint32_t g_current_frame;
bool vk_draw_frame(GLFWwindow *window, VkSurfaceKHR surface);
bool vk_draw_frame(void);
#endif // PTK_PTK_VK_DRAW_H_

View file

@ -29,8 +29,9 @@ PTK_LIST_DEFINE(voidptr);
PTK_LIST_DEFINE(VkDescriptorSet);
static VkInstance m_instance;
VkPhysicalDevice g_physical_dev;
static GLFWwindow *m_window = NULL;
GLFWwindow *g_window = NULL;
VkSurfaceKHR g_surface = VK_NULL_HANDLE;
@ -869,7 +870,7 @@ bool vk_update_uniform_buffer(const size_t current_frame) {
}
bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const char *title, const PtkVersion version) {
m_window = window;
g_window = window;
m_uniform_buffer_object.initial_window_size.w = width;
m_uniform_buffer_object.initial_window_size.h = height;
@ -882,19 +883,23 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const
m_instance = instance_opt.value;
if (!vk_select_physical_dev(m_instance)) {
PTK_OPTION(VkPhysicalDevice) physical_dev_opt = vk_select_physical_dev(m_instance);
if (!physical_dev_opt.exists) {
PTK_ERR("failed selecting physical device");
return false;
}
VK_TRY(false, glfwCreateWindowSurface(m_instance, m_window, NULL, &g_surface));
g_physical_dev = physical_dev_opt.value;
VK_TRY(false, glfwCreateWindowSurface(m_instance, g_window, NULL, &g_surface));
if (!vk_create_logical_dev(g_physical_dev, g_surface, m_validation_layers)) {
PTK_ERR("failed creating logical device");
return false;
}
if (!vk_create_swapchain(m_window, g_dev, g_physical_dev, g_surface)) {
if (!vk_create_swapchain(g_window, g_dev, g_physical_dev, g_surface)) {
PTK_ERR("failed creating swapchain");
return false;
}
@ -1006,7 +1011,7 @@ void vk_cleanup(void) {
vkDestroySurfaceKHR(m_instance, g_surface, NULL);
vkDestroyInstance(m_instance, NULL);
glfwDestroyWindow(m_window);
glfwDestroyWindow(g_window);
glfwTerminate();
}

View file

@ -14,6 +14,10 @@ PTK_LIST_DEFINE(VkCommandBuffer);
PTK_LIST_DEFINE(VkSemaphore);
PTK_LIST_DEFINE(VkFence);
extern VkPhysicalDevice g_physical_dev;
extern GLFWwindow *g_window;
extern VkQueue g_graphics_queue;
extern VkQueue g_present_queue;

View file

@ -6,14 +6,12 @@
#include <vulkan/vulkan.h>
VkPhysicalDevice g_physical_dev;
bool vk_select_physical_dev(const VkInstance instance) {
PTK_OPTION(VkPhysicalDevice) vk_select_physical_dev(const VkInstance instance) {
uint32_t dev_count = 0;
vkEnumeratePhysicalDevices(instance, &dev_count, NULL);
if (dev_count == 0) {
PTK_ERR("failed to find GPU with vulkan support");
return false;
return PTK_OPTION_NONE(VkPhysicalDevice);
}
VkPhysicalDevice devs[dev_count];
vkEnumeratePhysicalDevices(instance, &dev_count, devs);
@ -75,7 +73,5 @@ bool vk_select_physical_dev(const VkInstance instance) {
PTK_DEBUG("total memory: %lu", dev_mem_totals[dev_best_index]);
g_physical_dev = devs[dev_best_index];
return true;
return PTK_OPTION_SOME(VkPhysicalDevice, devs[dev_best_index]);
}

View file

@ -7,10 +7,8 @@
#include <ptk_option.h>
extern VkPhysicalDevice g_physical_dev;
PTK_OPTION_DEFINE(VkPhysicalDevice);
bool vk_select_physical_dev(const VkInstance instance);
PTK_OPTION(VkPhysicalDevice) vk_select_physical_dev(const VkInstance instance);
#endif // PTK_PTK_VK_PHYSICAL_DEVICE_H_

View file

@ -218,7 +218,7 @@ bool vk_create_swapchain(GLFWwindow *window, VkDevice dev, VkPhysicalDevice phys
return true;
}
bool vk_recreate_swapchain(GLFWwindow *window, VkSurfaceKHR surface) {
bool vk_recreate_swapchain(GLFWwindow *window, VkPhysicalDevice physical_dev, VkSurfaceKHR surface) {
int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
@ -231,7 +231,7 @@ bool vk_recreate_swapchain(GLFWwindow *window, VkSurfaceKHR surface) {
vk_cleanup_swapchain(g_dev);
if (!vk_create_swapchain(window, g_dev, g_physical_dev, surface)) {
if (!vk_create_swapchain(window, g_dev, physical_dev, surface)) {
PTK_ERR("failed creating new swapchain");
return false;
}

View file

@ -31,7 +31,7 @@ bool vk_create_swapchain(GLFWwindow *window, VkDevice dev, VkPhysicalDevice phys
bool vk_create_image_views(VkDevice dev);
bool vk_recreate_swapchain(GLFWwindow *window, VkSurfaceKHR surface);
bool vk_recreate_swapchain(GLFWwindow *window, VkPhysicalDevice physical_dev, VkSurfaceKHR surface);
bool vk_create_framebuffers(void);