1
Fork 0

Call on_resize gfx function in SDL backend, properly resize on vulkan

This commit is contained in:
Joshua Goins 2022-10-05 22:48:30 -04:00
parent 2a04bfa8bb
commit ad31684bd9
3 changed files with 45 additions and 21 deletions

View file

@ -5,7 +5,6 @@
struct GfxRenderingAPI; struct GfxRenderingAPI;
struct GfxWindowManagerAPI; struct GfxWindowManagerAPI;
struct Gfx;
struct GfxDimensions { struct GfxDimensions {
uint32_t width, height; uint32_t width, height;

View file

@ -20,6 +20,9 @@
#include "gfx_window_manager_api.h" #include "gfx_window_manager_api.h"
#include "gfx_screen_config.h" #include "gfx_screen_config.h"
#include <PR/gbi.h>
#include "gfx_pc.h"
#include "gfx_rendering_api.h"
static SDL_Window *wnd; static SDL_Window *wnd;
static int inverted_scancode_table[512]; 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) { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
window_width = event.window.data1; window_width = event.window.data1;
window_height = event.window.data2; window_height = event.window.data2;
gfx_get_current_rendering_api()->on_resize();
} }
break; break;
case SDL_QUIT: case SDL_QUIT:

View file

@ -9,6 +9,7 @@
#include <fstream> #include <fstream>
#include <glslang/Public/ShaderLang.h> #include <glslang/Public/ShaderLang.h>
#include <glslang/SPIRV/GlslangToSpv.h> #include <glslang/SPIRV/GlslangToSpv.h>
#include <PR/gbi.h>
#include "gfx_rendering_api.h" #include "gfx_rendering_api.h"
#include "gfx_pc.h" #include "gfx_pc.h"
@ -73,6 +74,10 @@ static std::array<int, 2> bound_textures;
static VkDescriptorPool descriptor_pool; static VkDescriptorPool descriptor_pool;
static uint32_t window_width, window_height;
static VkViewport last_viewport;
static VkRect2D last_scissor;
static int get_texture_hash() { static int get_texture_hash() {
int hash = 0; int hash = 0;
for(int i = 0; i < bound_textures.size(); i++) { for(int i = 0; i < bound_textures.size(); i++) {
@ -457,21 +462,10 @@ static std::tuple<VkPipeline, VkPipelineLayout, VkDescriptorSetLayout> gfx_vulka
input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
input_assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; 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 = {}; VkPipelineViewportStateCreateInfo viewport_state = {};
viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewport_state.viewportCount = 1; viewport_state.viewportCount = 1;
viewport_state.pViewports = &viewport;
viewport_state.scissorCount = 1; viewport_state.scissorCount = 1;
viewport_state.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo rasterizer = {}; VkPipelineRasterizationStateCreateInfo rasterizer = {};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@ -491,8 +485,12 @@ static std::tuple<VkPipeline, VkPipelineLayout, VkDescriptorSetLayout> gfx_vulka
color_blending.attachmentCount = 1; color_blending.attachmentCount = 1;
color_blending.pAttachments = &color_blend_attachment; color_blending.pAttachments = &color_blend_attachment;
const std::array dynamic_states = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VkPipelineDynamicStateCreateInfo dynamic_state = {}; VkPipelineDynamicStateCreateInfo dynamic_state = {};
dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; 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<VkDescriptorSetLayoutBinding> bindings; std::vector<VkDescriptorSetLayoutBinding> bindings;
@ -833,6 +831,8 @@ static void gfx_vulkan_create_swapchain() {
image_count = capabilities.maxImageCount; image_count = capabilities.maxImageCount;
} }
gfx_get_current_windowing_api()->get_dimensions(&window_width, &window_height);
// create swapchain // create swapchain
VkSwapchainCreateInfoKHR swapchain_create_info = {}; VkSwapchainCreateInfoKHR swapchain_create_info = {};
swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; 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.minImageCount = image_count;
swapchain_create_info.imageFormat = surface_format; swapchain_create_info.imageFormat = surface_format;
swapchain_create_info.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; // FIXME: hardcoded swapchain_create_info.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; // FIXME: hardcoded
swapchain_create_info.imageExtent.width = 640; // FIXME: hardcoded swapchain_create_info.imageExtent.width = window_width; // FIXME: hardcoded
swapchain_create_info.imageExtent.height = 480; swapchain_create_info.imageExtent.height = window_height;
swapchain_create_info.imageArrayLayers = 1; swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
@ -975,8 +975,8 @@ static void gfx_vulkan_create_depth() {
VkImageCreateInfo image_create_info = {}; VkImageCreateInfo image_create_info = {};
image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image_create_info.imageType = VK_IMAGE_TYPE_2D; image_create_info.imageType = VK_IMAGE_TYPE_2D;
image_create_info.extent.width = 640; image_create_info.extent.width = window_width;
image_create_info.extent.height = 480; image_create_info.extent.height = window_height;
image_create_info.extent.depth = 1; image_create_info.extent.depth = 1;
image_create_info.mipLevels = 1; image_create_info.mipLevels = 1;
image_create_info.arrayLayers = 1; image_create_info.arrayLayers = 1;
@ -1024,8 +1024,8 @@ static void gfx_vulkan_create_framebuffers() {
create_info.renderPass = render_pass; create_info.renderPass = render_pass;
create_info.attachmentCount = attachments.size(); create_info.attachmentCount = attachments.size();
create_info.pAttachments = attachments.data(); create_info.pAttachments = attachments.data();
create_info.width = 640; create_info.width = window_width;
create_info.height = 480; create_info.height = window_height;
create_info.layers = 1; create_info.layers = 1;
vkCreateFramebuffer(device, &create_info, nullptr, &swapchain_framebuffers[i]); 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) { 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; 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.clearValueCount = clear_values.size();
render_pass_begin.pClearValues = clear_values.data(); render_pass_begin.pClearValues = clear_values.data();
render_pass_begin.renderArea.extent.width = 640; render_pass_begin.renderArea.extent.width = window_width;
render_pass_begin.renderArea.extent.height = 480; render_pass_begin.renderArea.extent.height = window_height;
vkCmdBeginRenderPass(current_cmd, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); 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) { 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) { 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) { 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) { 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); 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())) { if(!last_program->cached_sets.count(get_texture_hash())) {
gfx_vulkan_cache_descriptor(); gfx_vulkan_cache_descriptor();
} }