Visual issue while resizing window

Hello. I am somewhat new to GLFW. I know rendering WHILE the window is resizing is a notorious problem. I have been struggling to get consistent rendering while the window is resizing. I am hoping to get some help. My machine is MacOS Sonoma, Intel i5.

Whenever I try rendering during a window resize (I use glfwSetFramebufferSizeCallback), I get this “stretchy” behavior. See the video.

It almost like the rendering is not keeping up with the speed of the resize. I also tried creating a separate thread for rendering but have seen the same thing. I am using bgfx in this example.

resize

#include <GLFW/glfw3.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include "debug.hpp"

#define GLFW_EXPOSE_NATIVE_COCOA
#include <GLFW/glfw3native.h>

bgfx::PlatformData pd;
bgfx::Init init;

static constexpr char NAME[] = "app";
static constexpr int INIT_WINDOW_WIDTH = 800;
static constexpr int INIT_WINDOW_HEIGHT = 600;

static constexpr int VIEWID = 0;

void glfw_error_callback(int error, const char* description) {
    DEBUG_PRINT("GLFW Error: %s\n", description);
}

void resize(GLFWwindow *window, int width, int height)
{
    bgfx::touch(VIEWID);
    bgfx::reset(width, height, BGFX_RESET_VSYNC);    
    bgfx::setViewClear(VIEWID, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x1495EDFF, 1.0f, 0);
    bgfx::setViewRect(VIEWID, 0, 0, 500, 500);
    bgfx::frame();
}

int main(void)
{
    glfwSetErrorCallback(glfw_error_callback);
    if (!glfwInit())
    {
        DEBUG_PRINT("Failed to initialize GLFW\n");
        return EXIT_FAILURE;
    }

    // Create a GLFW window
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    GLFWwindow* window = glfwCreateWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, NAME, nullptr, nullptr);
    if (!window)
    {
        DEBUG_PRINT("Failed to create GLFW window!\n");
        glfwTerminate();
        return EXIT_FAILURE;
    }

    glfwSetFramebufferSizeCallback(window, resize);

    // This call should be only used on platforms that don't
    // allow creating separate rendering thread. If it is called before
    // bgfx::init, render thread won't be created by bgfx::init call.
    bgfx::renderFrame();

    // initialize bgfx
    pd.nwh = (void*)glfwGetCocoaWindow(window);

    if (pd.nwh == nullptr)
    {
        DEBUG_PRINT("Failed to get valid window handle\n");
        glfwDestroyWindow(window);
        glfwTerminate();
        return EXIT_FAILURE;
    }

    init.type = bgfx::RendererType::Count; // auto choose renderer
    init.resolution.width = INIT_WINDOW_WIDTH;
    init.resolution.height = INIT_WINDOW_HEIGHT;
    init.resolution.reset = BGFX_RESET_VSYNC;
    init.platformData = pd;

    if (!bgfx::init(init))
    {
        DEBUG_PRINT("bgfx initialization failed\n");
        glfwDestroyWindow(window);
        glfwTerminate();
        return EXIT_FAILURE;
    }

    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        resize(window, width, height);
    }

    glfwDestroyWindow(window);
    glfwTerminate();

    return EXIT_SUCCESS;
}