From 5f0dfe9675b8722b2cdb88f3d06dc3da7c78ecb7 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Wed, 2 Jul 2025 22:00:52 +0200 Subject: [PATCH] Window resize fixes --- base/sources/backends/android_system.c | 4 +--- base/sources/backends/direct3d12_gpu.c | 2 +- base/sources/backends/ios_system.m | 4 +--- base/sources/backends/linux_system.c | 3 +-- base/sources/backends/macos_system.m | 16 +++++++++++++--- base/sources/backends/metal_gpu.m | 23 +++++++++++++++++++++-- base/sources/backends/vulkan_gpu.c | 2 +- base/sources/backends/windows_system.c | 3 +-- base/sources/iron_gpu.c | 4 ++++ base/sources/iron_gpu.h | 2 ++ 10 files changed, 46 insertions(+), 17 deletions(-) diff --git a/base/sources/backends/android_system.c b/base/sources/backends/android_system.c index d73cfd3f..c76d47a3 100644 --- a/base/sources/backends/android_system.c +++ b/base/sources/backends/android_system.c @@ -1070,8 +1070,6 @@ double iron_time() { return (double)(now.tv_sec - start_sec) + (now.tv_usec / 1000000.0); } -void gpu_internal_resize(int width, int height); - bool iron_internal_handle_messages(void) { iron_mutex_lock(&unicode_mutex); for (int i = 0; i < unicode_stack_index; ++i) { @@ -1114,7 +1112,7 @@ bool iron_internal_handle_messages(void) { activityJustResized = false; int32_t width = iron_android_width(); int32_t height = iron_android_height(); - gpu_internal_resize(width, height); + gpu_resize(width, height); iron_internal_call_resize_callback(width, height); } diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 78ad4473..42da3415 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -469,7 +469,7 @@ void gpu_present() { command_list->lpVtbl->Reset(command_list, command_allocator, NULL); } -void gpu_internal_resize(int width, int height) { +void gpu_resize_internal(int width, int height) { if (fence_value == 0 || width == 0 || height == 0) { return; } diff --git a/base/sources/backends/ios_system.m b/base/sources/backends/ios_system.m index 2e28ad10..62863ef0 100644 --- a/base/sources/backends/ios_system.m +++ b/base/sources/backends/ios_system.m @@ -112,9 +112,7 @@ void iron_internal_call_resize_callback(int width, int height); backingWidth = self.frame.size.width * self.contentScaleFactor; backingHeight = self.frame.size.height * self.contentScaleFactor; - CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer; - metalLayer.drawableSize = CGSizeMake(backingWidth, backingHeight); - + gpu_resize(); iron_internal_call_resize_callback(backingWidth, backingHeight); } diff --git a/base/sources/backends/linux_system.c b/base/sources/backends/linux_system.c index 6740bac0..548c20c4 100644 --- a/base/sources/backends/linux_system.c +++ b/base/sources/backends/linux_system.c @@ -473,7 +473,6 @@ static char *clipboardString = NULL; char buffer[1024]; -void gpu_internal_resize(int width, int height); static void init_pen_device(XDeviceInfo *info, struct x11_pen_device *pen, bool eraser); static void load_lib(void **lib, const char *name) { @@ -1198,7 +1197,7 @@ static bool _handle_messages() { if (event.xconfigure.width != k_window->width || event.xconfigure.height != k_window->height) { k_window->width = event.xconfigure.width; k_window->height = event.xconfigure.height; - gpu_internal_resize(event.xconfigure.width, event.xconfigure.height); + gpu_resize(event.xconfigure.width, event.xconfigure.height); iron_internal_call_resize_callback(event.xconfigure.width, event.xconfigure.height); } break; diff --git a/base/sources/backends/macos_system.m b/base/sources/backends/macos_system.m index 8ab5d7ff..59e824ab 100644 --- a/base/sources/backends/macos_system.m +++ b/base/sources/backends/macos_system.m @@ -1308,13 +1308,23 @@ int main(int argc, char **argv) { iron_stop(); } +void iron_internal_call_resize_callback(int width, int height) { + if (windows[0].resizeCallback != NULL) { + windows[0].resizeCallback(width, height, windows[0].resizeCallbackData); + } +} + - (void)windowDidResize:(NSNotification *)notification { NSWindow *window = [notification object]; NSSize size = [[window contentView] frame].size; [view resize:size]; - if (windows[0].resizeCallback != NULL) { - windows[0].resizeCallback(size.width, size.height, windows[0].resizeCallbackData); - } + + float scale = [window backingScaleFactor]; + int w = size.width * scale; + int h = size.height * scale; + + gpu_resize(w, h); + iron_internal_call_resize_callback(w, h); } - (void)windowWillMiniaturize:(NSNotification *)notification { diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index bf2d349d..593162f5 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -24,6 +24,8 @@ static id linear_sampler; static bool has_depth = false; static int argument_buffer_step; static gpu_buffer_t *current_index_buffer; +static int resize_w = 0; +static int resize_h = 0; static MTLBlendFactor convert_blending_factor(gpu_blending_factor_t factor) { switch (factor) { @@ -143,7 +145,6 @@ static int format_byte_size(gpu_texture_format_t format) { } void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int framebuffer_index) { - id device = getMetalDevice(); memset(target, 0, sizeof(gpu_texture_t)); target->width = width; target->height = height; @@ -171,7 +172,15 @@ void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_t void gpu_destroy(void) { } -void gpu_internal_resize(int width, int height) { +void gpu_resize_internal(int width, int height) { + if (width == 0 || height == 0) { + return; + } + if (width == framebuffers[0].width && height == framebuffers[0].height) { + return; + } + resize_w = width; + resize_h = height; } static void next_drawable() { @@ -317,6 +326,16 @@ void gpu_present() { command_buffer = nil; command_encoder = nil; + if (resize_w > 0) { + CAMetalLayer *layer = getMetalLayer(); + layer.drawableSize = CGSizeMake(resize_w, resize_h); + for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { + // gpu_texture_destroy(&framebuffers[i]); + gpu_render_target_init2(&framebuffers[i], resize_w, resize_h, GPU_TEXTURE_FORMAT_RGBA32, i); + } + resize_w = 0; + } + next_drawable(); } diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index deda9cc2..386e83b1 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -399,7 +399,7 @@ void create_descriptor_layout(void) { vkCreateDescriptorPool(device, &pool_info, NULL, &descriptor_pool); } -void gpu_internal_resize(int width, int height) { +void gpu_resize_internal(int width, int height) { // vkDeviceWaitIdle(device); // create_swapchain(); } diff --git a/base/sources/backends/windows_system.c b/base/sources/backends/windows_system.c index 582d3880..79ddafe7 100644 --- a/base/sources/backends/windows_system.c +++ b/base/sources/backends/windows_system.c @@ -408,7 +408,6 @@ void iron_mouse_get_position(int *x, int *y) { __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001; __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; -void gpu_internal_resize(int width, int height); typedef BOOL(WINAPI *GetPointerInfoType)(UINT32 pointerId, POINTER_INFO *pointerInfo); static GetPointerInfoType MyGetPointerInfo = NULL; @@ -653,7 +652,7 @@ LRESULT WINAPI IronWindowsMessageProcedure(HWND hWnd, UINT msg, WPARAM wParam, L case WM_SIZE: { int width = LOWORD(lParam); int height = HIWORD(lParam); - gpu_internal_resize(width, height); + gpu_resize(width, height); iron_internal_call_resize_callback(width, height); break; } diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index b2fdec66..16ce34be 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -74,6 +74,10 @@ void gpu_end() { gpu_end_internal(); } +void gpu_resize(int width, int height) { + gpu_resize_internal(width, height); +} + void gpu_set_int(int location, int value) { int *ints = (int *)(&constant_buffer.data[location]); ints[0] = value; diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index 84eb2dd5..6e3c2a67 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -154,6 +154,8 @@ void gpu_init(int depth_buffer_bits, bool vsync); void gpu_init_internal(int depth_buffer_bits, bool vsync); void gpu_destroy(void); void gpu_draw(void); +void gpu_resize(int width, int height); +void gpu_resize_internal(int width, int height); void gpu_set_int(int location, int value); void gpu_set_int2(int location, int value1, int value2); void gpu_set_int3(int location, int value1, int value2, int value3);