diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b8ce57..25b5dc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,7 +211,7 @@ if (NOT BB_IS_CI) set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_WIN32_KHR) target_compile_definitions(${COMPILER_NAME} PRIVATE GLFW_EXPOSE_NATIVE_WIN32) elseif(CMAKE_SYSTEM_NAME STREQUAL Linux) - set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_XCB_KHR) + set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_XLIB_KHR) target_compile_definitions(${COMPILER_NAME} PRIVATE GLFW_EXPOSE_NATIVE_X11) elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin) set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_METAL_EXT) diff --git a/bootstrap/bloat-buster/bb_core.c b/bootstrap/bloat-buster/bb_core.c index 67cf8e8..a15b773 100644 --- a/bootstrap/bloat-buster/bb_core.c +++ b/bootstrap/bloat-buster/bb_core.c @@ -209,8 +209,12 @@ void run_app() { state.arena = arena_init(MB(512), MB(2), MB(2)); - os_graphics_init((OSGraphicsInitializationOptions) { + os_windowing_init((OSWindowingInitializationOptions) { +#ifdef __linux__ .should_use_x11 = 1, +#else + .should_use_x11 = 0, +#endif }); state.renderer = renderer_initialize(state.arena); diff --git a/bootstrap/include/std/window.h b/bootstrap/include/std/window.h index 4f1f1e7..d66f926 100644 --- a/bootstrap/include/std/window.h +++ b/bootstrap/include/std/window.h @@ -119,7 +119,7 @@ typedef void OSWindowCursorEnter(OSWindow window, void* context, u8 entered); typedef void OSWindowScroll(OSWindow window, void* context, f64 x, f64 y); typedef void OSWindowDrop(OSWindow window, void* context, CStringSlice paths); -STRUCT(OSGraphicCallbacks) +STRUCT(OSWindowingCallbacks) { OSFramebufferResize* framebuffer_resize; OSWindowResize* window_resize; @@ -140,9 +140,9 @@ STRUCT(OSGraphicCallbacks) OSWindowDrop* window_drop; }; -STRUCT(OSGraphicsInitializationOptions) +STRUCT(OSWindowingInitializationOptions) { - OSGraphicCallbacks callback; + OSWindowingCallbacks callback; u8 should_use_x11; }; @@ -167,7 +167,7 @@ STRUCT(OSCursorPosition) f64 y; }; -EXPORT void os_graphics_init(OSGraphicsInitializationOptions options); +EXPORT void os_windowing_init(OSWindowingInitializationOptions options); EXPORT OSWindow os_window_create(OSWindowCreate create); EXPORT u8 os_window_should_close(OSWindow window); EXPORT void os_poll_events(OSEventQueue* event_queue); @@ -188,3 +188,5 @@ EXPORT Window x11_window_get(OSWindow window); #ifdef _WIN32 EXPORT HANDLE win32_window_get(OSWindow window); #endif + +int window_create_surface(void* instance, OSWindow window, const void* allocator, void** surface); diff --git a/bootstrap/std/renderer_vulkan.c b/bootstrap/std/renderer_vulkan.c index 498d26f..3ac58c6 100644 --- a/bootstrap/std/renderer_vulkan.c +++ b/bootstrap/std/renderer_vulkan.c @@ -1565,13 +1565,13 @@ fn void swapchain_recreate(Renderer* renderer, RenderWindow* window) }); } -typedef void GLFWwindow; -extern VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); +// typedef void GLFWwindow; +// extern VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); RenderWindow* renderer_window_initialize(Renderer* renderer, OSWindow window) { RenderWindow* result = &renderer_window_memory; - vkok(glfwCreateWindowSurface(renderer->instance, window, renderer->allocator, &result->surface)); + vkok((VkResult)window_create_surface(renderer->instance, window, renderer->allocator, (void**)&result->surface)); swapchain_recreate(renderer, result); diff --git a/bootstrap/std/window.c b/bootstrap/std/window.c index d4688e1..c289496 100644 --- a/bootstrap/std/window.c +++ b/bootstrap/std/window.c @@ -7,10 +7,11 @@ #include #include -global_variable OSGraphicCallbacks callbacks; +global_variable OSWindowingCallbacks callbacks; // TODO: thread local global_variable OSEventQueue* event_queue = 0; +global_variable u8 use_x11 = 0; fn void monitor_callback(GLFWmonitor* monitor, int event) { @@ -41,11 +42,16 @@ fn void bitset_list_add(VirtualBuffer(OSEventBitset)* list, u32* counter, u64 va list->pointer[bitset_index].value |= (value << bit_index); } -void os_graphics_init(OSGraphicsInitializationOptions options) +void os_windowing_init(OSWindowingInitializationOptions options) { #ifdef __linux__ + use_x11 = options.should_use_x11; int platform_hint = options.should_use_x11 ? GLFW_PLATFORM_X11 : GLFW_PLATFORM_WAYLAND; glfwInitHint(GLFW_PLATFORM, platform_hint); + if (platform_hint == GLFW_PLATFORM_X11) + { + glfwInitHint(GLFW_X11_XCB_VULKAN_SURFACE, GLFW_FALSE); + } #endif if (glfwInit() != GLFW_TRUE) @@ -427,3 +433,28 @@ OSCursorPosition os_window_cursor_position_get(OSWindow window) glfwGetCursorPos(window, &result.x, &result.y); return result; } + +int window_create_surface(void* instance, OSWindow window, const void* allocator, void** surface) +{ +#define FORCE_XLIB_INITIALIZATION 1 + auto* surface_pointer = (VkSurfaceKHR*)surface; + if (use_x11 && FORCE_XLIB_INITIALIZATION) + { + auto* x11_display = glfwGetX11Display(); + auto x11_window = glfwGetX11Window(window); + VkXlibSurfaceCreateInfoKHR create_info = { + .sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, + .pNext = 0, + .flags = 0, + .dpy = x11_display, + .window = x11_window, + }; + + return vkCreateXlibSurfaceKHR(instance, &create_info, allocator, surface_pointer); + } + else + { + return glfwCreateWindowSurface(instance, window, allocator, surface_pointer); + } +} +