From e3713249767a8a64a30d93ab5fa221c289d00dd5 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Thu, 26 Sep 2024 23:33:59 +0200 Subject: [PATCH] what the fuck was that???????? --- src/ptk_vk/device.c | 2 -- src/ptk_vk/device.h | 2 -- src/ptk_vk/init.c | 81 +++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/ptk_vk/device.c b/src/ptk_vk/device.c index 0eeb9bb..e03b022 100644 --- a/src/ptk_vk/device.c +++ b/src/ptk_vk/device.c @@ -14,8 +14,6 @@ #include #include -VkDevice g_dev; - VkQueue g_graphics_queue; VkQueue g_present_queue; diff --git a/src/ptk_vk/device.h b/src/ptk_vk/device.h index a868eb9..daea122 100644 --- a/src/ptk_vk/device.h +++ b/src/ptk_vk/device.h @@ -8,8 +8,6 @@ #include -extern VkDevice g_dev; - PTK_OPTION_DEFINE(VkDevice); // for PTK_ARRAY(constcharptr) diff --git a/src/ptk_vk/init.c b/src/ptk_vk/init.c index 1b3042c..3282911 100644 --- a/src/ptk_vk/init.c +++ b/src/ptk_vk/init.c @@ -23,6 +23,7 @@ #include static VkInstance m_instance; +static VkDevice m_dev; static VkPhysicalDevice m_physical_dev; static GLFWwindow *m_window; @@ -76,7 +77,7 @@ bool vk_transfer_vertex_data(void) { PTK_DEBUG("transferring vertices to gpu…"); const size_t vertices_size = sizeof(g_vertices.data[0]) * g_vertices.size; - if (!transfer_to_buffer(g_dev, m_physical_dev, m_command_pool, g_vertices.data, vertices_size, m_vertex_buffer, g_graphics_queue)) { + if (!transfer_to_buffer(m_dev, m_physical_dev, m_command_pool, g_vertices.data, vertices_size, m_vertex_buffer, g_graphics_queue)) { PTK_ERR("failed transferring vertices"); return false; } @@ -88,7 +89,7 @@ bool vk_transfer_vertex_data(void) { PTK_DEBUG("transferring indices to gpu…"); const size_t indices_size = sizeof(g_indices.data[0]) * g_indices.size; - if (!transfer_to_buffer(g_dev, m_physical_dev, m_command_pool, g_indices.data, indices_size, m_index_buffer, g_graphics_queue)) { + if (!transfer_to_buffer(m_dev, m_physical_dev, m_command_pool, g_indices.data, indices_size, m_index_buffer, g_graphics_queue)) { PTK_ERR("failed transferring indices"); return false; } @@ -196,19 +197,19 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const return false; } - g_dev = dev_opt.value; + m_dev = dev_opt.value; - if (!vk_create_swapchain(m_window, g_dev, m_physical_dev, m_surface)) { + if (!vk_create_swapchain(m_window, m_dev, m_physical_dev, m_surface)) { PTK_ERR("failed creating swapchain"); return false; } - if (!vk_create_image_views(g_dev)) { + if (!vk_create_image_views(m_dev)) { PTK_ERR("failed creating image views"); return false; } - PTK_OPTION(VkRenderPass) render_pass_opt = vk_create_render_pass(g_dev, g_swapchain_image_format); + PTK_OPTION(VkRenderPass) render_pass_opt = vk_create_render_pass(m_dev, g_swapchain_image_format); if (!render_pass_opt.exists) { PTK_ERR("failed creating render pass"); @@ -217,7 +218,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_render_pass = render_pass_opt.value; - PTK_OPTION(VkDescriptorSetLayout) descriptor_set_layout_opt = vk_create_descriptor_set_layout(g_dev); + PTK_OPTION(VkDescriptorSetLayout) descriptor_set_layout_opt = vk_create_descriptor_set_layout(m_dev); if (!descriptor_set_layout_opt.exists) { PTK_ERR("failed creating descriptor set layout"); @@ -226,7 +227,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_descriptor_set_layout = descriptor_set_layout_opt.value; - PTK_OPTION(PipelineStuff) pipeline_stuff_opt = vk_create_pipeline(g_dev, m_render_pass, m_descriptor_set_layout, g_swapchain_extent); + PTK_OPTION(PipelineStuff) pipeline_stuff_opt = vk_create_pipeline(m_dev, m_render_pass, m_descriptor_set_layout, g_swapchain_extent); if (!pipeline_stuff_opt.exists) { PTK_ERR("failed creating graphics pipeline"); @@ -236,12 +237,12 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_pipeline = pipeline_stuff_opt.value.pipeline; m_pipeline_layout = pipeline_stuff_opt.value.pipeline_layout; - if (!vk_create_framebuffers(g_dev, m_render_pass)) { + if (!vk_create_framebuffers(m_dev, m_render_pass)) { PTK_ERR("failed creating framebuffers"); return false; } - PTK_OPTION(VkCommandPool) command_pool_opt = vk_create_command_pool(g_dev, g_queue_family_indices.graphics.value); + PTK_OPTION(VkCommandPool) command_pool_opt = vk_create_command_pool(m_dev, g_queue_family_indices.graphics.value); if (!command_pool_opt.exists) { PTK_ERR("failed creating command pool"); @@ -253,7 +254,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const const VkDeviceSize buffer_size = 65536; PTK_OPTION(BufferStuff) vertex_buffer_stuff_opt = create_buffer( - g_dev, + m_dev, m_physical_dev, buffer_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, @@ -269,7 +270,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_vertex_buffer_memory = vertex_buffer_stuff_opt.value.buffer_memory; PTK_OPTION(BufferStuff) index_buffer_stuff_opt = create_buffer( - g_dev, + m_dev, m_physical_dev, buffer_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, @@ -284,7 +285,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_index_buffer = index_buffer_stuff_opt.value.buffer; m_index_buffer_memory = index_buffer_stuff_opt.value.buffer_memory; - PTK_OPTION(UniformBufferStuff) uniform_buffer_stuff_opt = vk_create_uniform_buffers(g_dev, m_physical_dev, m_max_frames_in_flight); + PTK_OPTION(UniformBufferStuff) uniform_buffer_stuff_opt = vk_create_uniform_buffers(m_dev, m_physical_dev, m_max_frames_in_flight); if (!uniform_buffer_stuff_opt.exists) { PTK_ERR("failed creating uniform buffers"); @@ -295,7 +296,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_uniform_buffer_memories = uniform_buffer_stuff_opt.value.buffer_memories; m_uniform_buffers_mapped = uniform_buffer_stuff_opt.value.buffers_mapped; - PTK_OPTION(VkDescriptorPool) descriptor_pool_opt = vk_create_descriptor_pool(g_dev, m_max_frames_in_flight); + PTK_OPTION(VkDescriptorPool) descriptor_pool_opt = vk_create_descriptor_pool(m_dev, m_max_frames_in_flight); if (!descriptor_pool_opt.exists) { PTK_ERR("failed creating descriptor pool"); @@ -304,7 +305,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_descriptor_pool = descriptor_pool_opt.value; - PTK_OPTION(VkDescriptorSetList) descriptor_sets_opt = vk_create_descriptor_sets(g_dev, m_descriptor_set_layout, m_descriptor_pool, m_uniform_buffers, m_max_frames_in_flight); + PTK_OPTION(VkDescriptorSetList) descriptor_sets_opt = vk_create_descriptor_sets(m_dev, m_descriptor_set_layout, m_descriptor_pool, m_uniform_buffers, m_max_frames_in_flight); if (!descriptor_sets_opt.exists) { PTK_ERR("failed creating descriptor sets"); @@ -313,7 +314,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_descriptor_sets = descriptor_sets_opt.value; - PTK_OPTION(VkCommandBufferList) command_buffers_opt = vk_allocate_command_buffers(g_dev, m_command_pool, m_max_frames_in_flight); + PTK_OPTION(VkCommandBufferList) command_buffers_opt = vk_allocate_command_buffers(m_dev, m_command_pool, m_max_frames_in_flight); if (!command_buffers_opt.exists) { PTK_ERR("failed allocating command buffers"); @@ -322,7 +323,7 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_command_buffers = command_buffers_opt.value; - PTK_OPTION(SyncObjects) sync_objects_opt = vk_create_sync_objects(g_dev, m_max_frames_in_flight); + PTK_OPTION(SyncObjects) sync_objects_opt = vk_create_sync_objects(m_dev, m_max_frames_in_flight); if (!sync_objects_opt.exists) { PTK_ERR("failed creating sync objects"); @@ -353,11 +354,11 @@ uint32_t vk_current_frame(void) { } bool vk_draw_frame(void) { - vkWaitForFences(g_dev, 1, &m_in_flight_fences.data[m_current_frame], VK_TRUE, UINT64_MAX); + vkWaitForFences(m_dev, 1, &m_in_flight_fences.data[m_current_frame], VK_TRUE, UINT64_MAX); uint32_t image_index; const VkResult acquire_next_image_result = vkAcquireNextImageKHR( - g_dev, + m_dev, g_swapchain, UINT64_MAX, m_image_available_semaphores.data[m_current_frame], @@ -366,7 +367,7 @@ bool vk_draw_frame(void) { ); if (acquire_next_image_result == VK_ERROR_OUT_OF_DATE_KHR) { - if (!vk_recreate_swapchain(m_window, g_dev, m_physical_dev, m_surface, m_render_pass)) { + if (!vk_recreate_swapchain(m_window, m_dev, m_physical_dev, m_surface, m_render_pass)) { return false; } return true; @@ -377,7 +378,7 @@ bool vk_draw_frame(void) { vk_update_uniform_buffer(m_current_frame); - vkResetFences(g_dev, 1, &m_in_flight_fences.data[m_current_frame]); + vkResetFences(m_dev, 1, &m_in_flight_fences.data[m_current_frame]); vkResetCommandBuffer(m_command_buffers.data[m_current_frame], 0); if (!vk_record_command_buffer(m_command_buffers.data[m_current_frame], image_index)) { @@ -423,7 +424,7 @@ bool vk_draw_frame(void) { m_framebuffer_resized ) { m_framebuffer_resized = false; - if (!vk_recreate_swapchain(m_window, g_dev, m_physical_dev, m_surface, m_render_pass)) { + if (!vk_recreate_swapchain(m_window, m_dev, m_physical_dev, m_surface, m_render_pass)) { return false; } PTK_TRACE("recreated swapchain"); @@ -438,47 +439,47 @@ bool vk_draw_frame(void) { } void vk_finish(void) { - vkDeviceWaitIdle(g_dev); + vkDeviceWaitIdle(m_dev); vk_cleanup(); vk_components_cleanup(); } void vk_cleanup(void) { - vk_cleanup_swapchain(g_dev); + vk_cleanup_swapchain(m_dev); - vkDestroyPipeline(g_dev, m_pipeline, NULL); - vkDestroyPipelineLayout(g_dev, m_pipeline_layout, NULL); + vkDestroyPipeline(m_dev, m_pipeline, NULL); + vkDestroyPipelineLayout(m_dev, m_pipeline_layout, NULL); - vkDestroyRenderPass(g_dev, m_render_pass, NULL); + vkDestroyRenderPass(m_dev, m_render_pass, NULL); for (size_t i = 0; i < m_max_frames_in_flight; ++i) { - vkDestroyBuffer(g_dev, m_uniform_buffers.data[i], NULL); - vkFreeMemory(g_dev, m_uniform_buffer_memories.data[i], NULL); + vkDestroyBuffer(m_dev, m_uniform_buffers.data[i], NULL); + vkFreeMemory(m_dev, m_uniform_buffer_memories.data[i], NULL); } PTK_LIST_FREE(m_uniform_buffers); PTK_LIST_FREE(m_uniform_buffer_memories); PTK_LIST_FREE(m_uniform_buffers_mapped); - vkDestroyDescriptorPool(g_dev, m_descriptor_pool, NULL); + vkDestroyDescriptorPool(m_dev, m_descriptor_pool, NULL); - vkDestroyDescriptorSetLayout(g_dev, m_descriptor_set_layout, NULL); + vkDestroyDescriptorSetLayout(m_dev, m_descriptor_set_layout, NULL); - vkDestroyBuffer(g_dev, m_index_buffer, NULL); - vkFreeMemory(g_dev, m_index_buffer_memory, NULL); + vkDestroyBuffer(m_dev, m_index_buffer, NULL); + vkFreeMemory(m_dev, m_index_buffer_memory, NULL); - vkDestroyBuffer(g_dev, m_vertex_buffer, NULL); - vkFreeMemory(g_dev, m_vertex_buffer_memory, NULL); + vkDestroyBuffer(m_dev, m_vertex_buffer, NULL); + vkFreeMemory(m_dev, m_vertex_buffer_memory, NULL); for (size_t i = 0; i < m_max_frames_in_flight; ++i) { - vkDestroySemaphore(g_dev, m_image_available_semaphores.data[i], NULL); - vkDestroySemaphore(g_dev, m_render_finished_semaphores.data[i], NULL); - vkDestroyFence(g_dev, m_in_flight_fences.data[i], NULL); + vkDestroySemaphore(m_dev, m_image_available_semaphores.data[i], NULL); + vkDestroySemaphore(m_dev, m_render_finished_semaphores.data[i], NULL); + vkDestroyFence(m_dev, m_in_flight_fences.data[i], NULL); } - vkDestroyCommandPool(g_dev, m_command_pool, NULL); + vkDestroyCommandPool(m_dev, m_command_pool, NULL); - vkDestroyDevice(g_dev, NULL); + vkDestroyDevice(m_dev, NULL); vkDestroySurfaceKHR(m_instance, m_surface, NULL); vkDestroyInstance(m_instance, NULL);