Skip to content

Commit 9c86393

Browse files
cube: Support runtime selection of WSI platform
By changing the selection of a WSI platform from a build time choice to a runtime choice, this allows the removal of vkcube-wayland as a separate binary. Use `--wsi <xlib|xcb|wayland|display|directfb>` to choose the WSI platform that vkcube will render with. The chosen platform must be one that vkcube was compiled with support for, so directfb will be unavailable without changing the build parameters to enable it. These changes have been made to both vkcube and vkcubepp. Platforms which do not have multiple WSI platforms, such as windows, android, and apple (metal is the only non-deprecated platform) do not have this option as there aren't any platforms to choose from.
1 parent 4c63e84 commit 9c86393

File tree

7 files changed

+1957
-818
lines changed

7 files changed

+1957
-818
lines changed

.github/workflows/tools.yml

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ jobs:
7979
run: |
8080
cmake -S. -B build -G "Ninja" \
8181
-D CMAKE_BUILD_TYPE=${{matrix.config}} \
82-
-D CUBE_WSI_SELECTION=${{matrix.cube_wsi}} \
8382
-D UPDATE_DEPS=ON \
8483
-D BUILD_WERROR=ON \
8584
-D INSTALL_ICD=ON \

cube/CMakeLists.txt

+120-89
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,55 @@ if (CMAKE_SYSTEM_NAME MATCHES "Linux|BSD|GNU")
6666
option(BUILD_WSI_XLIB_SUPPORT "Build Xlib WSI support" ON)
6767
option(BUILD_WSI_WAYLAND_SUPPORT "Build Wayland WSI support" ON)
6868
option(BUILD_WSI_DIRECTFB_SUPPORT "Build DirectFB WSI support" OFF)
69-
set(CUBE_WSI_SELECTION "XCB" CACHE STRING "Select WSI target for vkcube (XCB, XLIB, WAYLAND, DIRECTFB, DISPLAY)")
69+
option(BUILD_WSI_DISPLAY_SUPPORT "Build DISPLAY WSI support" ON)
7070

7171
find_package(PkgConfig REQUIRED QUIET) # Use PkgConfig to find Linux system libraries
7272

7373
if(BUILD_WSI_XCB_SUPPORT)
7474
pkg_check_modules(XCB REQUIRED QUIET IMPORTED_TARGET xcb)
75+
pkg_get_variable(XCB_INCLUDE_DIRS xcb includedir)
76+
message(DEBUG "XCB_INCLUDE_DIRS = ${XCB_INCLUDE_DIRS}")
7577
endif()
7678

7779
if(BUILD_WSI_XLIB_SUPPORT)
7880
pkg_check_modules(X11 REQUIRED QUIET IMPORTED_TARGET x11)
81+
pkg_get_variable(XLIB_INCLUDE_DIRS x11 includedir)
82+
message(DEBUG "XLIB_INCLUDE_DIRS = ${XLIB_INCLUDE_DIRS}")
7983
endif()
8084

8185
if(BUILD_WSI_WAYLAND_SUPPORT)
8286
pkg_check_modules(WAYLAND_CLIENT REQUIRED IMPORTED_TARGET wayland-client)
87+
pkg_get_variable(WAYLAND_INCLUDE_DIRS wayland-client includedir)
8388

8489
pkg_get_variable(WAYLAND_SCANNER_EXECUTABLE wayland-scanner wayland_scanner)
85-
message(STATUS "WAYLAND_SCANNER_EXECUTABLE = ${WAYLAND_SCANNER_EXECUTABLE}")
90+
message(DEBUG "WAYLAND_SCANNER_EXECUTABLE = ${WAYLAND_SCANNER_EXECUTABLE}")
91+
92+
pkg_get_variable(WAYLAND_CLIENT_PATH wayland-client pkgdatadir)
93+
message(DEBUG "WAYLAND_CLIENT_PATH = ${WAYLAND_CLIENT_PATH}")
94+
set(WAYLAND_CODE_PROTOCOL ${WAYLAND_CLIENT_PATH}/wayland.xml)
8695

8796
pkg_get_variable(WAYLAND_PROTOCOLS_PATH wayland-protocols pkgdatadir)
88-
message(STATUS "WAYLAND_PROTOCOLS_PATH = ${WAYLAND_PROTOCOLS_PATH}")
97+
message(DEBUG "WAYLAND_PROTOCOLS_PATH = ${WAYLAND_PROTOCOLS_PATH}")
8998
set(XDG_SHELL_PROTOCOL ${WAYLAND_PROTOCOLS_PATH}/stable/xdg-shell/xdg-shell.xml)
99+
100+
add_custom_command(COMMENT "Generating wayland client protocol dispatch data"
101+
OUTPUT wayland-client.c
102+
COMMAND ${WAYLAND_SCANNER_EXECUTABLE}
103+
private-code
104+
${WAYLAND_CODE_PROTOCOL}
105+
${CMAKE_CURRENT_BINARY_DIR}/wayland-client.c
106+
MAIN_DEPENDENCY ${WAYLAND_CODE_PROTOCOL}
107+
DEPENDS ${WAYLAND_CODE_PROTOCOL} ${WAYLAND_SCANNER_EXECUTABLE})
108+
109+
add_custom_command(COMMENT "Generating wayland client protocol header"
110+
OUTPUT wayland-client-header.h
111+
COMMAND ${WAYLAND_SCANNER_EXECUTABLE}
112+
client-header
113+
${WAYLAND_CODE_PROTOCOL}
114+
${CMAKE_CURRENT_BINARY_DIR}/wayland-client-header.h
115+
MAIN_DEPENDENCY ${WAYLAND_CODE_PROTOCOL}
116+
DEPENDS ${WAYLAND_CODE_PROTOCOL} ${WAYLAND_SCANNER_EXECUTABLE})
117+
90118
add_custom_command(COMMENT "Generating xdg-shell protocol dispatch data"
91119
OUTPUT xdg-shell-code.c
92120
COMMAND ${WAYLAND_SCANNER_EXECUTABLE}
@@ -135,45 +163,23 @@ elseif(ANDROID)
135163
elseif(APPLE)
136164
add_definitions(-DVK_USE_PLATFORM_METAL_EXT)
137165
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|BSD|GNU")
138-
if(NOT CUBE_WSI_SELECTION)
139-
set(CUBE_WSI_SELECTION "XCB")
166+
if(BUILD_WSI_XCB_SUPPORT)
167+
list(APPEND ENABLED_CUBE_PLATFORMS VK_USE_PLATFORM_XCB_KHR)
140168
endif()
141-
142-
if(CUBE_WSI_SELECTION STREQUAL "XCB")
143-
if(NOT BUILD_WSI_XCB_SUPPORT)
144-
message(FATAL_ERROR "Selected XCB for vkcube build but not building Xcb support")
145-
endif()
146-
link_libraries(PkgConfig::XCB)
147-
set(CUBE_PLATFORM VK_USE_PLATFORM_XCB_KHR)
148-
elseif(CUBE_WSI_SELECTION STREQUAL "XLIB")
149-
if(NOT BUILD_WSI_XLIB_SUPPORT)
150-
message(FATAL_ERROR "Selected XLIB for vkcube build but not building Xlib support")
151-
endif()
152-
link_libraries(PkgConfig::X11)
153-
set(CUBE_PLATFORM VK_USE_PLATFORM_XLIB_KHR)
154-
elseif(CUBE_WSI_SELECTION STREQUAL "WAYLAND")
155-
if(NOT BUILD_WSI_WAYLAND_SUPPORT)
156-
message(FATAL_ERROR "Selected Wayland for vkcube build but not building Wayland support")
157-
endif()
158-
link_libraries(PkgConfig::WAYLAND_CLIENT)
159-
set(CUBE_PLATFORM VK_USE_PLATFORM_WAYLAND_KHR)
160-
set(XDG_SHELL_PROTOCOL ${WAYLAND_PROTOCOLS_PATH}/stable/xdg-shell/xdg-shell.xml)
161-
set(OPTIONAL_WAYLAND_DATA_FILES
162-
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-code.c
163-
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-header.h
164-
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-code.c
165-
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-client-header.h)
166-
include_directories(${CMAKE_CURRENT_BINARY_DIR})
167-
elseif(CUBE_WSI_SELECTION STREQUAL "DIRECTFB")
168-
if(NOT BUILD_WSI_DIRECTFB_SUPPORT)
169-
message(FATAL_ERROR "Selected DIRECTFB for vkcube build but not building DirectFB support")
170-
endif()
171-
link_libraries(PkgConfig::DirectFB)
172-
set(CUBE_PLATFORM VK_USE_PLATFORM_DIRECTFB_EXT)
173-
elseif(CUBE_WSI_SELECTION STREQUAL "DISPLAY")
174-
set(CUBE_PLATFORM VK_USE_PLATFORM_DISPLAY_KHR)
175-
else()
176-
message(FATAL_ERROR "Unrecognized value for CUBE_WSI_SELECTION: ${CUBE_WSI_SELECTION}")
169+
if(BUILD_WSI_XLIB_SUPPORT)
170+
list(APPEND ENABLED_CUBE_PLATFORMS VK_USE_PLATFORM_XLIB_KHR)
171+
endif()
172+
if(BUILD_WSI_WAYLAND_SUPPORT)
173+
list(APPEND ENABLED_CUBE_PLATFORMS VK_USE_PLATFORM_WAYLAND_KHR)
174+
endif()
175+
if(BUILD_WSI_DIRECTFB_SUPPORT)
176+
list(APPEND ENABLED_CUBE_PLATFORMS VK_USE_PLATFORM_DIRECTFB_EXT)
177+
endif()
178+
if(BUILD_WSI_DISPLAY_SUPPORT)
179+
list(APPEND ENABLED_CUBE_PLATFORMS VK_USE_PLATFORM_DISPLAY_KHR)
180+
endif()
181+
if(NOT DEFINED ENABLED_CUBE_PLATFORMS)
182+
message(FATAL_ERROR "There are no supported WSI platforms on this system, vkcube requires a WSI platform be available to be able to render its output")
177183
endif()
178184

179185
link_libraries(${API_LOWERCASE} m)
@@ -226,7 +232,6 @@ elseif (ANDROID)
226232

227233
add_subdirectory(android)
228234

229-
target_link_libraries(vkcube PRIVATE Vulkan::Headers volk::volk_headers)
230235
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|BSD|GNU")
231236
add_executable(vkcube)
232237
target_sources(vkcube PRIVATE
@@ -235,15 +240,35 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|BSD|GNU")
235240
${PROJECT_SOURCE_DIR}/cube/cube.frag
236241
cube.vert.inc
237242
cube.frag.inc
238-
${OPTIONAL_WAYLAND_DATA_FILES}
239243
)
240-
target_compile_definitions(vkcube PUBLIC ${CUBE_PLATFORM})
244+
target_link_libraries(vkcube PRIVATE Threads::Threads)
245+
if(BUILD_WSI_XCB_SUPPORT)
246+
target_sources(vkcube PRIVATE xcb_loader.h)
247+
target_include_directories(vkcube PRIVATE ${xcb_INCLUDE_DIRS})
248+
endif()
249+
if(BUILD_WSI_XLIB_SUPPORT)
250+
target_sources(vkcube PRIVATE xlib_loader.h)
251+
target_include_directories(vkcube PRIVATE ${XLIB_INCLUDE_DIRS})
252+
endif()
253+
if(BUILD_WSI_WAYLAND_SUPPORT)
254+
target_include_directories(vkcube PRIVATE ${WAYLAND_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
255+
target_sources(vkcube PRIVATE
256+
${CMAKE_CURRENT_BINARY_DIR}/wayland-client-header.h
257+
${CMAKE_CURRENT_BINARY_DIR}/wayland-client.c
258+
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-code.c
259+
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-header.h
260+
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-code.c
261+
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-client-header.h
262+
)
263+
endif()
264+
if(BUILD_WSI_DIRECTFB_SUPPORT)
265+
target_link_libraries(vkcube PRIVATE PkgConfig::DirectFB)
266+
endif()
241267
include(CheckLibraryExists)
242268
CHECK_LIBRARY_EXISTS("rt" clock_gettime "" NEED_RT)
243269
if (NEED_RT)
244270
target_link_libraries(vkcube PRIVATE rt)
245271
endif()
246-
target_link_libraries(vkcube PRIVATE Vulkan::Headers volk::volk_headers Threads::Threads)
247272
elseif(WIN32)
248273
add_executable(vkcube WIN32)
249274
target_sources(vkcube PRIVATE
@@ -253,12 +278,13 @@ elseif(WIN32)
253278
cube.vert.inc
254279
cube.frag.inc
255280
)
256-
target_link_libraries(vkcube PRIVATE Vulkan::Headers volk::volk_headers)
257281
else()
258282
message(FATAL_ERROR "Unsupported Platform!")
259283
endif()
260284

285+
target_compile_definitions(vkcube PRIVATE ${ENABLED_CUBE_PLATFORMS})
261286
target_include_directories(vkcube PRIVATE .)
287+
target_link_libraries(vkcube PRIVATE Vulkan::Headers volk::volk_headers)
262288

263289
if (ANDROID)
264290
install(TARGETS vkcube DESTINATION ${CMAKE_INSTALL_LIBDIR})
@@ -280,6 +306,16 @@ if (ANDROID)
280306
return()
281307
endif()
282308

309+
if (XCB_LINK_LIBRARIES)
310+
target_compile_definitions(vkcube PRIVATE "XCB_LIBRARY=\"${XCB_LINK_LIBRARIES}\"")
311+
endif()
312+
if (X11_LINK_LIBRARIES)
313+
target_compile_definitions(vkcube PRIVATE "XLIB_LIBRARY=\"${X11_LINK_LIBRARIES}\"")
314+
endif()
315+
if (WAYLAND_CLIENT_LINK_LIBRARIES)
316+
target_compile_definitions(vkcube PRIVATE "WAYLAND_LIBRARY=\"${WAYLAND_CLIENT_LINK_LIBRARIES}\"")
317+
endif()
318+
283319
# ----------------------------------------------------------------------------
284320
# vkcubepp
285321

@@ -291,10 +327,31 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|BSD|GNU")
291327
${PROJECT_SOURCE_DIR}/cube/cube.vert
292328
${PROJECT_SOURCE_DIR}/cube/cube.frag
293329
cube.vert.inc
294-
cube.frag.inc
295-
${OPTIONAL_WAYLAND_DATA_FILES})
296-
target_link_libraries(vkcubepp Vulkan::Headers volk::volk_headers Threads::Threads)
297-
target_compile_definitions(vkcubepp PUBLIC ${CUBE_PLATFORM})
330+
cube.frag.inc)
331+
target_link_libraries(vkcubepp PRIVATE Threads::Threads)
332+
333+
if(BUILD_WSI_XCB_SUPPORT)
334+
target_sources(vkcubepp PRIVATE xcb_loader.h)
335+
target_include_directories(vkcubepp PRIVATE ${xcb_INCLUDE_DIRS})
336+
endif()
337+
if(BUILD_WSI_XLIB_SUPPORT)
338+
target_sources(vkcubepp PRIVATE xlib_loader.h)
339+
target_include_directories(vkcubepp PRIVATE ${XLIB_INCLUDE_DIRS})
340+
endif()
341+
if(BUILD_WSI_WAYLAND_SUPPORT)
342+
target_include_directories(vkcubepp PRIVATE ${WAYLAND_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
343+
target_sources(vkcubepp PRIVATE
344+
${CMAKE_CURRENT_BINARY_DIR}/wayland-client-header.h
345+
${CMAKE_CURRENT_BINARY_DIR}/wayland-client.c
346+
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-code.c
347+
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-header.h
348+
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-code.c
349+
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-client-header.h
350+
)
351+
endif()
352+
if(BUILD_WSI_DIRECTFB_SUPPORT)
353+
target_link_libraries(vkcubepp PRIVATE PkgConfig::DirectFB)
354+
endif()
298355
else()
299356
add_executable(vkcubepp
300357
WIN32
@@ -303,9 +360,21 @@ else()
303360
${PROJECT_SOURCE_DIR}/cube/cube.frag
304361
cube.vert.inc
305362
cube.frag.inc)
306-
target_link_libraries(vkcubepp Vulkan::Headers volk::volk_headers)
307363
endif()
364+
308365
target_include_directories(vkcubepp PRIVATE .)
366+
target_compile_definitions(vkcubepp PRIVATE ${ENABLED_CUBE_PLATFORMS})
367+
target_link_libraries(vkcubepp PRIVATE Vulkan::Headers volk::volk_headers)
368+
369+
if (XCB_LINK_LIBRARIES )
370+
target_compile_definitions(vkcubepp PUBLIC "XCB_LIBRARY=\"${XCB_LINK_LIBRARIES}\"")
371+
endif()
372+
if (X11_LINK_LIBRARIES)
373+
target_compile_definitions(vkcubepp PUBLIC "XLIB_LIBRARY=\"${X11_LINK_LIBRARIES}\"")
374+
endif()
375+
if (WAYLAND_CLIENT_LINK_LIBRARIES)
376+
target_compile_definitions(vkcubepp PUBLIC "WAYLAND_LIBRARY=\"${WAYLAND_CLIENT_LINK_LIBRARIES}\"")
377+
endif()
309378

310379
if(APPLE)
311380
install(
@@ -320,41 +389,3 @@ if(APPLE)
320389
else()
321390
install(TARGETS vkcubepp)
322391
endif()
323-
324-
# ----------------------------------------------------------------------------
325-
# vkcube-wayland
326-
327-
if (CMAKE_SYSTEM_NAME MATCHES "Linux|BSD")
328-
if(BUILD_WSI_WAYLAND_SUPPORT AND EXISTS ${WAYLAND_PROTOCOLS_PATH}/unstable/xdg-decoration/xdg-decoration-unstable-v1.xml)
329-
add_executable(vkcube-wayland)
330-
331-
target_sources(vkcube-wayland PRIVATE
332-
cube.c
333-
${PROJECT_SOURCE_DIR}/cube/cube.vert
334-
${PROJECT_SOURCE_DIR}/cube/cube.frag
335-
cube.vert.inc
336-
cube.frag.inc
337-
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-code.c
338-
${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-header.h
339-
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-code.c
340-
${CMAKE_CURRENT_BINARY_DIR}/xdg-decoration-client-header.h
341-
)
342-
target_include_directories(vkcube-wayland PRIVATE
343-
${CMAKE_CURRENT_BINARY_DIR}
344-
.
345-
)
346-
target_link_libraries(vkcube-wayland PRIVATE
347-
Vulkan::Headers
348-
volk::volk_headers
349-
Threads::Threads
350-
PkgConfig::WAYLAND_CLIENT
351-
)
352-
target_compile_definitions(vkcube-wayland PRIVATE VK_USE_PLATFORM_WAYLAND_KHR)
353-
include(CheckLibraryExists)
354-
CHECK_LIBRARY_EXISTS("rt" clock_gettime "" NEED_RT)
355-
if (NEED_RT)
356-
target_link_libraries(vkcube-wayland PRIVATE rt)
357-
endif()
358-
install(TARGETS vkcube-wayland)
359-
endif()
360-
endif()

0 commit comments

Comments
 (0)