almost improve physical dev module

draw function needs some help
This commit is contained in:
jacekpoz 2024-09-22 20:23:10 +02:00
parent 7565731ef5
commit 402e45c766
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
8 changed files with 21 additions and 22 deletions

View file

@ -197,7 +197,7 @@ int ptk_run(PtkHandle root) {
while (!glfwWindowShouldClose(m_window)) {
glfwPollEvents();
if (!vk_draw_frame(m_window, g_surface)) {
if (!vk_draw_frame(m_window, /*missing physical_dev*/ g_surface)) {
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(GLFWwindow *window, VkPhysicalDevice physical_dev, VkSurfaceKHR surface) {
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(window, physical_dev, 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(window, physical_dev, 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(GLFWwindow *window, VkPhysicalDevice physical_dev, VkSurfaceKHR surface);
#endif // PTK_PTK_VK_DRAW_H_

View file

@ -29,6 +29,7 @@ PTK_LIST_DEFINE(voidptr);
PTK_LIST_DEFINE(VkDescriptorSet);
static VkInstance m_instance;
static VkPhysicalDevice m_physical_dev;
static GLFWwindow *m_window = NULL;
@ -420,7 +421,7 @@ bool create_command_pool(void) {
PTK_OPTION(uint32_t) find_memory_type(const uint32_t type_filter, const VkMemoryPropertyFlags props) {
VkPhysicalDeviceMemoryProperties mem_props;
vkGetPhysicalDeviceMemoryProperties(g_physical_dev, &mem_props);
vkGetPhysicalDeviceMemoryProperties(m_physical_dev, &mem_props);
for (uint32_t i = 0; i < mem_props.memoryTypeCount; ++i) {
if (type_filter & (1 << i) && (mem_props.memoryTypes[i].propertyFlags & props) == props) {
@ -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;
}
m_physical_dev = physical_dev_opt.value;
VK_TRY(false, glfwCreateWindowSurface(m_instance, m_window, NULL, &g_surface));
if (!vk_create_logical_dev(g_physical_dev, g_surface, m_validation_layers)) {
if (!vk_create_logical_dev(m_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(m_window, g_dev, m_physical_dev, g_surface)) {
PTK_ERR("failed creating swapchain");
return false;
}

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);