bgfx init hangs without errors (MacOS)

I am trying to make a “Hello Quad” with bgfx and sdl2. I was able to open a window with sdl no problem, but when I tried adding simple bgfx starter my program hangs with no errors. Here is the full code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <fstream>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <bx/math.h>
template <typename T>
void print(T t)
{
std::cout << t << std::endl;
}
template <typename T, typename... Args>
void print(T t, Args... args)
{
std::cout << t << " ";
print(args...);
}
int main(int argc, char *argv[])
{
print("program start");
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}
print("sdl inited");
// Create a window
SDL_Window *window = SDL_CreateWindow("bgfx with SDL2", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
print("window created");
// Get the window's native handle
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(window, &wmi))
{
std::cerr << "Could not get window information! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
print("got window native handle");
// Set up the bgfx platform data with the native window handle
bgfx::PlatformData pd;
#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
pd.nwh = (void *)(uintptr_t)wmi.info.x11.window;
#elif BX_PLATFORM_OSX
pd.nwh = wmi.info.cocoa.window;
#elif BX_PLATFORM_WINDOWS
pd.nwh = wmi.info.win.window;
#endif
print("connected bgfx and sdl");
pd.ndt = nullptr;
pd.context = nullptr;
pd.backBuffer = nullptr;
pd.backBufferDS = nullptr;
print("prepared pd");
bgfx::setPlatformData(pd);
print("set pd");
// Initialize bgfx
bgfx::Init init;
print("created bgfx init object");
init.debug = BGFX_DEBUG_TEXT;
init.type = bgfx::RendererType::Count; // Automatically choose the renderer
init.resolution.width = 800;
init.resolution.height = 600;
init.resolution.reset = BGFX_RESET_VSYNC;
print("set init object props");
if (!bgfx::init(init))
{
std::cerr << "Failed to initialize bgfx!" << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
print("bgfx inited");
...
// main loop
return 0;
}
</code>
<code>#include <iostream> #include <fstream> #include <vector> #include <SDL2/SDL.h> #include <SDL2/SDL_syswm.h> #include <bgfx/bgfx.h> #include <bgfx/platform.h> #include <bx/math.h> template <typename T> void print(T t) { std::cout << t << std::endl; } template <typename T, typename... Args> void print(T t, Args... args) { std::cout << t << " "; print(args...); } int main(int argc, char *argv[]) { print("program start"); // Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; return 1; } print("sdl inited"); // Create a window SDL_Window *window = SDL_CreateWindow("bgfx with SDL2", 100, 100, 800, 600, SDL_WINDOW_SHOWN); if (window == nullptr) { std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } print("window created"); // Get the window's native handle SDL_SysWMinfo wmi; SDL_VERSION(&wmi.version); if (!SDL_GetWindowWMInfo(window, &wmi)) { std::cerr << "Could not get window information! SDL_Error: " << SDL_GetError() << std::endl; SDL_DestroyWindow(window); SDL_Quit(); return 1; } print("got window native handle"); // Set up the bgfx platform data with the native window handle bgfx::PlatformData pd; #if BX_PLATFORM_LINUX || BX_PLATFORM_BSD pd.nwh = (void *)(uintptr_t)wmi.info.x11.window; #elif BX_PLATFORM_OSX pd.nwh = wmi.info.cocoa.window; #elif BX_PLATFORM_WINDOWS pd.nwh = wmi.info.win.window; #endif print("connected bgfx and sdl"); pd.ndt = nullptr; pd.context = nullptr; pd.backBuffer = nullptr; pd.backBufferDS = nullptr; print("prepared pd"); bgfx::setPlatformData(pd); print("set pd"); // Initialize bgfx bgfx::Init init; print("created bgfx init object"); init.debug = BGFX_DEBUG_TEXT; init.type = bgfx::RendererType::Count; // Automatically choose the renderer init.resolution.width = 800; init.resolution.height = 600; init.resolution.reset = BGFX_RESET_VSYNC; print("set init object props"); if (!bgfx::init(init)) { std::cerr << "Failed to initialize bgfx!" << std::endl; SDL_DestroyWindow(window); SDL_Quit(); return 1; } print("bgfx inited"); ... // main loop return 0; } </code>
#include <iostream>
#include <fstream>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>

#include <bx/math.h>

template <typename T>
void print(T t)
{
    std::cout << t << std::endl;
}

template <typename T, typename... Args>
void print(T t, Args... args)
{
    std::cout << t << " ";
    print(args...);
}

int main(int argc, char *argv[])
{
    print("program start");

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    print("sdl inited");

    // Create a window
    SDL_Window *window = SDL_CreateWindow("bgfx with SDL2", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
    if (window == nullptr)
    {
        std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    print("window created");

    // Get the window's native handle
    SDL_SysWMinfo wmi;
    SDL_VERSION(&wmi.version);
    if (!SDL_GetWindowWMInfo(window, &wmi))
    {
        std::cerr << "Could not get window information! SDL_Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    print("got window native handle");

    // Set up the bgfx platform data with the native window handle
    bgfx::PlatformData pd;
#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
    pd.nwh = (void *)(uintptr_t)wmi.info.x11.window;
#elif BX_PLATFORM_OSX
    pd.nwh = wmi.info.cocoa.window;
#elif BX_PLATFORM_WINDOWS
    pd.nwh = wmi.info.win.window;
#endif

    print("connected bgfx and sdl");

    pd.ndt = nullptr;
    pd.context = nullptr;
    pd.backBuffer = nullptr;
    pd.backBufferDS = nullptr;

    print("prepared pd");

    bgfx::setPlatformData(pd);

    print("set pd");

    // Initialize bgfx
    bgfx::Init init;

    print("created bgfx init object");

    init.debug = BGFX_DEBUG_TEXT;
    init.type = bgfx::RendererType::Count; // Automatically choose the renderer
    init.resolution.width = 800;
    init.resolution.height = 600;
    init.resolution.reset = BGFX_RESET_VSYNC;

    print("set init object props");

    if (!bgfx::init(init))
    {
        std::cerr << "Failed to initialize bgfx!" << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    print("bgfx inited");

...
// main loop

    return 0;
}

The last debug statement that was printed to console is “set init object props”… Not the error message in case of failure or the next debug message in case of success.
So I compiled the project (including sdl and bgfx) in debug mode and ran it with lldb and here is what I got:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>program start
sdl inited
window created
got window native handle
connected bgfx and sdl
prepared pd
set pd
created bgfx init object
set init object props
2024-08-19 12:58:15.890213-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3417): BGFX bgfx platform data like window handle or backbuffer is not set, creating headless device.
2024-08-19 12:58:15.890234-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3449): BGFX Init...
2024-08-19 12:58:15.890248-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3456): BGFX Version 1.108.7238 (commit: 8065659e90a2673dd2ac4b12f193604a631833e3)
2024-08-19 12:58:15.891525-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1833): BGFX Creating rendering thread.
2024-08-19 12:58:15.891593-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1842): BGFX Running in multi-threaded mode
2024-08-19 12:58:15.891618-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx_p.h (2926): BGFX render thread start
2024-08-19 12:58:15.892255-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/renderer_mtl.mm (379): BGFX Init.
</code>
<code>program start sdl inited window created got window native handle connected bgfx and sdl prepared pd set pd created bgfx init object set init object props 2024-08-19 12:58:15.890213-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3417): BGFX bgfx platform data like window handle or backbuffer is not set, creating headless device. 2024-08-19 12:58:15.890234-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3449): BGFX Init... 2024-08-19 12:58:15.890248-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3456): BGFX Version 1.108.7238 (commit: 8065659e90a2673dd2ac4b12f193604a631833e3) 2024-08-19 12:58:15.891525-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1833): BGFX Creating rendering thread. 2024-08-19 12:58:15.891593-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1842): BGFX Running in multi-threaded mode 2024-08-19 12:58:15.891618-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx_p.h (2926): BGFX render thread start 2024-08-19 12:58:15.892255-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/renderer_mtl.mm (379): BGFX Init. </code>
program start
sdl inited
window created
got window native handle
connected bgfx and sdl
prepared pd
set pd
created bgfx init object
set init object props
2024-08-19 12:58:15.890213-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3417): BGFX bgfx platform data like window handle or backbuffer is not set, creating headless device.
2024-08-19 12:58:15.890234-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3449): BGFX Init...
2024-08-19 12:58:15.890248-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3456): BGFX Version 1.108.7238 (commit: 8065659e90a2673dd2ac4b12f193604a631833e3)
2024-08-19 12:58:15.891525-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1833): BGFX Creating rendering thread.
2024-08-19 12:58:15.891593-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1842): BGFX Running in multi-threaded mode
2024-08-19 12:58:15.891618-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx_p.h (2926): BGFX render thread start
2024-08-19 12:58:15.892255-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/renderer_mtl.mm (379): BGFX Init.

and that’s it… it hangs. And when I pause execution during the hang:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Process 35299 stopped
* thread #1, name = 'bgfx - renderer backend thread', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
frame #0: 0x000000018b26d7f0 libsystem_kernel.dylib`semaphore_wait_trap + 8
libsystem_kernel.dylib`semaphore_wait_trap:
-> 0x18b26d7f0 <+8>: ret
libsystem_kernel.dylib`semaphore_wait_signal_trap:
0x18b26d7f4 <+0>: mov x16, #-0x25
0x18b26d7f8 <+4>: svc #0x80
0x18b26d7fc <+8>: ret
Target 0: (HelloQuad) stopped.
</code>
<code>Process 35299 stopped * thread #1, name = 'bgfx - renderer backend thread', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP frame #0: 0x000000018b26d7f0 libsystem_kernel.dylib`semaphore_wait_trap + 8 libsystem_kernel.dylib`semaphore_wait_trap: -> 0x18b26d7f0 <+8>: ret libsystem_kernel.dylib`semaphore_wait_signal_trap: 0x18b26d7f4 <+0>: mov x16, #-0x25 0x18b26d7f8 <+4>: svc #0x80 0x18b26d7fc <+8>: ret Target 0: (HelloQuad) stopped. </code>
Process 35299 stopped
* thread #1, name = 'bgfx - renderer backend thread', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x000000018b26d7f0 libsystem_kernel.dylib`semaphore_wait_trap + 8
libsystem_kernel.dylib`semaphore_wait_trap:
->  0x18b26d7f0 <+8>: ret    

libsystem_kernel.dylib`semaphore_wait_signal_trap:
    0x18b26d7f4 <+0>: mov    x16, #-0x25
    0x18b26d7f8 <+4>: svc    #0x80
    0x18b26d7fc <+8>: ret    
Target 0: (HelloQuad) stopped.

I also found this from 10 years ago:
https://github.com/bkaradzic/bgfx/issues/501, which says to add this directive: “BGFX_CONFIG_MULTITHREADED=0”
So I tried it but it did not work – interestingly, the bgfx debug messages still said the same thing: “Running in multi-threaded mode”, “render thread start”, so I am not sure if that’s because a 10 year old directive just might not do what it used to or am I messing up the compilation/linking (it was a bit complicated because I used the CMake not GENie version), but, since the code compiles and links perfectly fine, and I was able to open an sdl window just fine I ruled out that option.

I don’t really know how to continue debugging this…

New contributor

BogdanB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật