From ad31684bd9d465f6a2d9b403e3c00611c65c3ec6 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Wed, 5 Oct 2022 22:48:30 -0400 Subject: [PATCH] Call on_resize gfx function in SDL backend, properly resize on vulkan --- src/pc/gfx/gfx_pc.h | 1 - src/pc/gfx/gfx_sdl2.c | 5 ++++ src/pc/gfx/gfx_vulkan.cpp | 60 ++++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/pc/gfx/gfx_pc.h b/src/pc/gfx/gfx_pc.h index 2d7ba80..737502b 100644 --- a/src/pc/gfx/gfx_pc.h +++ b/src/pc/gfx/gfx_pc.h @@ -5,7 +5,6 @@ struct GfxRenderingAPI; struct GfxWindowManagerAPI; -struct Gfx; struct GfxDimensions { uint32_t width, height; diff --git a/src/pc/gfx/gfx_sdl2.c b/src/pc/gfx/gfx_sdl2.c index bb011e9..be3e64a 100644 --- a/src/pc/gfx/gfx_sdl2.c +++ b/src/pc/gfx/gfx_sdl2.c @@ -20,6 +20,9 @@ #include "gfx_window_manager_api.h" #include "gfx_screen_config.h" +#include +#include "gfx_pc.h" +#include "gfx_rendering_api.h" static SDL_Window *wnd; static int inverted_scancode_table[512]; @@ -265,6 +268,8 @@ static void gfx_sdl_handle_events(void) { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { window_width = event.window.data1; window_height = event.window.data2; + + gfx_get_current_rendering_api()->on_resize(); } break; case SDL_QUIT: diff --git a/src/pc/gfx/gfx_vulkan.cpp b/src/pc/gfx/gfx_vulkan.cpp index eb51104..830f29d 100644 --- a/src/pc/gfx/gfx_vulkan.cpp +++ b/src/pc/gfx/gfx_vulkan.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "gfx_rendering_api.h" #include "gfx_pc.h" @@ -73,6 +74,10 @@ static std::array bound_textures; static VkDescriptorPool descriptor_pool; +static uint32_t window_width, window_height; +static VkViewport last_viewport; +static VkRect2D last_scissor; + static int get_texture_hash() { int hash = 0; for(int i = 0; i < bound_textures.size(); i++) { @@ -457,21 +462,10 @@ static std::tuple gfx_vulka input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; input_assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; - VkViewport viewport = {}; - viewport.width = 640; - viewport.height = 480; - viewport.maxDepth = 1.0f; - - VkRect2D scissor = {}; - scissor.extent.width = 640; - scissor.extent.height = 480; - VkPipelineViewportStateCreateInfo viewport_state = {}; viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewport_state.viewportCount = 1; - viewport_state.pViewports = &viewport; viewport_state.scissorCount = 1; - viewport_state.pScissors = &scissor; VkPipelineRasterizationStateCreateInfo rasterizer = {}; rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; @@ -491,8 +485,12 @@ static std::tuple gfx_vulka color_blending.attachmentCount = 1; color_blending.pAttachments = &color_blend_attachment; + const std::array dynamic_states = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; + VkPipelineDynamicStateCreateInfo dynamic_state = {}; dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; + dynamic_state.dynamicStateCount = dynamic_states.size(); + dynamic_state.pDynamicStates = dynamic_states.data(); std::vector bindings; @@ -833,6 +831,8 @@ static void gfx_vulkan_create_swapchain() { image_count = capabilities.maxImageCount; } + gfx_get_current_windowing_api()->get_dimensions(&window_width, &window_height); + // create swapchain VkSwapchainCreateInfoKHR swapchain_create_info = {}; swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; @@ -840,8 +840,8 @@ static void gfx_vulkan_create_swapchain() { swapchain_create_info.minImageCount = image_count; swapchain_create_info.imageFormat = surface_format; swapchain_create_info.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; // FIXME: hardcoded - swapchain_create_info.imageExtent.width = 640; // FIXME: hardcoded - swapchain_create_info.imageExtent.height = 480; + swapchain_create_info.imageExtent.width = window_width; // FIXME: hardcoded + swapchain_create_info.imageExtent.height = window_height; swapchain_create_info.imageArrayLayers = 1; swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; @@ -975,8 +975,8 @@ static void gfx_vulkan_create_depth() { VkImageCreateInfo image_create_info = {}; image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; image_create_info.imageType = VK_IMAGE_TYPE_2D; - image_create_info.extent.width = 640; - image_create_info.extent.height = 480; + image_create_info.extent.width = window_width; + image_create_info.extent.height = window_height; image_create_info.extent.depth = 1; image_create_info.mipLevels = 1; image_create_info.arrayLayers = 1; @@ -1024,8 +1024,8 @@ static void gfx_vulkan_create_framebuffers() { create_info.renderPass = render_pass; create_info.attachmentCount = attachments.size(); create_info.pAttachments = attachments.data(); - create_info.width = 640; - create_info.height = 480; + create_info.width = window_width; + create_info.height = window_height; create_info.layers = 1; vkCreateFramebuffer(device, &create_info, nullptr, &swapchain_framebuffers[i]); @@ -1153,7 +1153,9 @@ static void gfx_vulkan_renderer_init(void) { } static void gfx_vulkan_renderer_on_resize(void) { - // doesn't seem to be called? + gfx_vulkan_create_swapchain(); + gfx_vulkan_create_depth(); + gfx_vulkan_create_framebuffers(); } static int buf_vbo_offset = 0; @@ -1198,8 +1200,8 @@ static void gfx_vulkan_renderer_start_frame(void) { render_pass_begin.clearValueCount = clear_values.size(); render_pass_begin.pClearValues = clear_values.data(); - render_pass_begin.renderArea.extent.width = 640; - render_pass_begin.renderArea.extent.height = 480; + render_pass_begin.renderArea.extent.width = window_width; + render_pass_begin.renderArea.extent.height = window_height; vkCmdBeginRenderPass(current_cmd, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); @@ -1436,9 +1438,24 @@ static void gfx_vulkan_renderer_set_zmode_decal(bool zmode_decal) { } static void gfx_vulkan_renderer_set_viewport(int x, int y, int width, int height) { + VkViewport viewport = {}; + viewport.x = x; + viewport.y = y; + viewport.maxDepth = 1.0f; + viewport.width = width; + viewport.height = height; + + last_viewport = viewport; } static void gfx_vulkan_renderer_set_scissor(int x, int y, int width, int height) { + VkRect2D scissor = {}; + scissor.extent.width = width; + scissor.extent.height = height; + scissor.offset.x = x; + scissor.offset.y = y; + + last_scissor = scissor; } static void gfx_vulkan_renderer_set_use_alpha(bool use_alpha) { @@ -1457,6 +1474,9 @@ static void gfx_vulkan_renderer_load_shader(struct ShaderProgram *new_prg) { static void gfx_vulkan_renderer_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_t buf_vbo_num_tris) { vkCmdBindPipeline(current_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, last_program->pipeline); + vkCmdSetViewport(current_cmd, 0, 1, &last_viewport); + vkCmdSetScissor(current_cmd, 0, 1, &last_scissor); + if(!last_program->cached_sets.count(get_texture_hash())) { gfx_vulkan_cache_descriptor(); }