Skip to content

GDK CMake support (Xbox Series X only for now) #13185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 140 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ endif()
# See docs/release_checklist.md
project(SDL3 LANGUAGES C VERSION "3.3.0")

if(GDK_GAMING_XBOX)
# FIXME: Setting the standard of individual files doesn't seem to work properly
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I left this comment I realized that the standard for individual files cannot be changed, which is a shame. Hence the only solution is either leaving this or creating a separate target with the individual files that need to be set to C++17, but that makes it complicated as they're all mixed with the Windows support...

set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(SDL3_MAINPROJECT ON)
else()
Expand Down Expand Up @@ -331,8 +336,8 @@ dep_option(SDL_DISKAUDIO "Support the disk writer audio driver" ON "SD
dep_option(SDL_DUMMYAUDIO "Support the dummy audio driver" ON "SDL_AUDIO" OFF)
dep_option(SDL_DUMMYVIDEO "Use dummy video driver" ON "SDL_VIDEO" OFF)
dep_option(SDL_IBUS "Enable IBus support" ON "${UNIX_SYS}" OFF)
dep_option(SDL_OPENGL "Include OpenGL support" ON "SDL_VIDEO;NOT IOS;NOT VISIONOS;NOT TVOS;NOT WATCHOS" OFF)
dep_option(SDL_OPENGLES "Include OpenGL ES support" ON "SDL_VIDEO;NOT VISIONOS;NOT TVOS;NOT WATCHOS" OFF)
dep_option(SDL_OPENGL "Include OpenGL support" ON "SDL_VIDEO;NOT IOS;NOT VISIONOS;NOT TVOS;NOT WATCHOS;NOT GDK_GAMING_XBOX" OFF)
dep_option(SDL_OPENGLES "Include OpenGL ES support" ON "SDL_VIDEO;NOT VISIONOS;NOT TVOS;NOT WATCHOS;NOT GDK_GAMING_XBOX" OFF)
set_option(SDL_PTHREADS "Use POSIX threads for multi-threading" ${SDL_PTHREADS_DEFAULT})
dep_option(SDL_PTHREADS_SEM "Use pthread semaphores" ON "SDL_PTHREADS" OFF)
dep_option(SDL_OSS "Support the OSS audio API" ${SDL_OSS_DEFAULT} "UNIX_SYS OR RISCOS;SDL_AUDIO" OFF)
Expand Down Expand Up @@ -375,8 +380,8 @@ dep_option(SDL_RENDER_D3D12 "Enable the Direct3D 12 render driver" ON "SD
dep_option(SDL_RENDER_METAL "Enable the Metal render driver" ON "SDL_RENDER;${APPLE}" OFF)
dep_option(SDL_RENDER_GPU "Enable the SDL_GPU render driver" ON "SDL_RENDER;SDL_GPU" OFF)
dep_option(SDL_VIVANTE "Use Vivante EGL video driver" ON "${UNIX_SYS};SDL_CPU_ARM32" OFF)
dep_option(SDL_VULKAN "Enable Vulkan support" ON "SDL_VIDEO;ANDROID OR APPLE OR LINUX OR FREEBSD OR WINDOWS" OFF)
dep_option(SDL_RENDER_VULKAN "Enable the Vulkan render driver" ON "SDL_RENDER;SDL_VULKAN" OFF)
dep_option(SDL_VULKAN "Enable Vulkan support" ON "SDL_VIDEO;ANDROID OR APPLE OR LINUX OR FREEBSD OR WINDOWS;NOT GDK_GAMING_XBOX" OFF)
dep_option(SDL_RENDER_VULKAN "Enable the Vulkan render driver" ON "SDL_RENDER;SDL_VULKAN;NOT GDK_GAMING_XBOX" OFF)
dep_option(SDL_METAL "Enable Metal support" ON "APPLE" OFF)
set_option(SDL_OPENVR "Use OpenVR video driver" OFF)
dep_option(SDL_KMSDRM "Use KMS DRM video driver" ${UNIX_SYS} "SDL_VIDEO" OFF)
Expand All @@ -385,11 +390,11 @@ set_option(SDL_OFFSCREEN "Use offscreen video driver" ON)
dep_option(SDL_DUMMYCAMERA "Support the dummy camera driver" ON SDL_CAMERA OFF)
option_string(SDL_BACKGROUNDING_SIGNAL "number to use for magic backgrounding signal or 'OFF'" OFF)
option_string(SDL_FOREGROUNDING_SIGNAL "number to use for magic foregrounding signal or 'OFF'" OFF)
dep_option(SDL_HIDAPI "Enable the HIDAPI subsystem" ON "NOT VISIONOS" OFF)
dep_option(SDL_HIDAPI "Enable the HIDAPI subsystem" ON "NOT VISIONOS;NOT GDK_GAMING_XBOX" OFF)
dep_option(SDL_HIDAPI_LIBUSB "Use libusb for low level joystick drivers" ON SDL_HIDAPI_LIBUSB_AVAILABLE OFF)
dep_option(SDL_HIDAPI_LIBUSB_SHARED "Dynamically load libusb support" ON "SDL_HIDAPI_LIBUSB;SDL_DEPS_SHARED" OFF)
dep_option(SDL_HIDAPI_JOYSTICK "Use HIDAPI for low level joystick drivers" ON SDL_HIDAPI OFF)
dep_option(SDL_VIRTUAL_JOYSTICK "Enable the virtual-joystick driver" ON SDL_HIDAPI OFF)
dep_option(SDL_VIRTUAL_JOYSTICK "Enable the virtual-joystick driver" ON "SDL_HIDAPI OR GDK_GAMING_XBOX" OFF)
set_option(SDL_LIBUDEV "Enable libudev support" ON)
set_option(SDL_ASAN "Use AddressSanitizer to detect memory errors" OFF)
set_option(SDL_CCACHE "Use Ccache to speed up build" OFF)
Expand Down Expand Up @@ -441,6 +446,32 @@ if(SDL_PRESEED)
SDL_Preseed_CMakeCache()
endif()

if(GDK_GAMING_XBOX)
if(GDK_GAMING_XBOX_ONE)
message(STATUS "Building shader blobs (Xbox One)")
execute_process(
COMMAND cmd.exe /Q /c "${SDL3_SOURCE_DIR}/src/render/direct3d12/compile_shaders_xbox.bat ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} one"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
execute_process(
COMMAND cmd.exe /Q /c "${SDL3_SOURCE_DIR}/src/gpu/d3d12/compile_shaders_xbox.bat ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} one"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
elseif(GDK_GAMING_XBOX_SCARLETT)
message(STATUS "Building shader blobs (Xbox Series)")
execute_process(
COMMAND cmd.exe /Q /c "${SDL3_SOURCE_DIR}/src/render/direct3d12/compile_shaders_xbox.bat ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
execute_process(
COMMAND cmd.exe /Q /c "${SDL3_SOURCE_DIR}/src/gpu/d3d12/compile_shaders_xbox.bat ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
else()
message(FATAL_ERROR "Unknown Xbox system")
endif()
endif()

if(SDL_SHARED)
add_library(SDL3-shared SHARED)
add_library(SDL3::SDL3-shared ALIAS SDL3-shared)
Expand Down Expand Up @@ -1055,6 +1086,10 @@ if(SDL_LIBC)
strcasestr
)
endif()
if(GDK_GAMING_XBOX)
list(REMOVE_ITEM symbols_to_check itoa)
endif()

check_library_exists(m pow "" HAVE_LIBM)
cmake_push_check_state()
if(HAVE_LIBM)
Expand Down Expand Up @@ -1922,6 +1957,9 @@ elseif(WINDOWS)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/core/windows/*.cpp")
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/main/windows/*.c")
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/io/windows/*.c")
if(GDK_GAMING_XBOX)
sdl_sources("${SDL3_SOURCE_DIR}/src/main/gdk/SDL_sysmain_runapp.cpp")
endif()

if(TARGET SDL3-shared AND MSVC AND NOT SDL_LIBC)
# Prevent codegen that would use the VC runtime libraries.
Expand Down Expand Up @@ -1955,10 +1993,19 @@ elseif(WINDOWS)
check_include_file(ddraw.h HAVE_DDRAW_H)
check_include_file(dsound.h HAVE_DSOUND_H)
check_include_file(dinput.h HAVE_DINPUT_H)
check_include_file(dxgi.h HAVE_DXGI_H)
# FIXME: Not sure why, avoiding the above still keeps "HAVE_DDRAW_H" and friends being 1?
if(GDK_GAMING_XBOX)
set(HAVE_D3D9_H 0)
set(HAVE_D3D11_H 0)
set(HAVE_DDRAW_H 0)
set(HAVE_DSOUND_H 0)
set(HAVE_DINPUT_H 0)
set(HAVE_DXGI_H 0)
endif()
if(SDL_CPU_ARM32) # !!! FIXME: this should probably check if we're !(x86 or x86-64) instead of arm.
set(HAVE_DINPUT_H 0)
endif()
check_include_file(dxgi.h HAVE_DXGI_H)
cmake_pop_check_state()
if(HAVE_D3D9_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H)
set(HAVE_DIRECTX TRUE)
Expand Down Expand Up @@ -1989,6 +2036,10 @@ elseif(WINDOWS)
static __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics2 *s2;
int main(int argc, char **argv) { return 0; }" HAVE_WINDOWS_GAMING_INPUT_H
)
# FIXME: Avoiding the check above still marks it as 1
if(GDK_GAMING_XBOX)
set(HAVE_WINDOWS_GAMING_INPUT_H 0)
endif()
check_c_source_compiles("
#include <stdbool.h>
#define COBJMACROS
Expand All @@ -2003,6 +2054,11 @@ elseif(WINDOWS)
check_include_file(sensorsapi.h HAVE_SENSORSAPI_H)
check_include_file(shellscalingapi.h HAVE_SHELLSCALINGAPI_H)
check_include_file(shobjidl_core.h HAVE_SHOBJIDL_CORE_H)
# FIXME: removing the checks didn't work?
if(GDK_GAMING_XBOX)
set(HAVE_ROAPI_H 0)
set(HAVE_SHELLSCALINGAPI_H 0)
endif()
check_c_source_compiles("
#include <windows.h>
#include <mfapi.h>
Expand Down Expand Up @@ -2034,17 +2090,35 @@ elseif(WINDOWS)

CheckOpenVR()

if(SDL_RENDER_D3D AND HAVE_D3D9_H)
if(SDL_RENDER_D3D AND HAVE_D3D9_H AND NOT GDK_GAMING_XBOX)
set(SDL_VIDEO_RENDER_D3D 1)
set(HAVE_RENDER_D3D TRUE)
endif()
if(SDL_RENDER_D3D11 AND HAVE_D3D11_H)
if(GDK_GAMING_XBOX)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/render/direct3d/SDL_render_d3d.c" PROPERTIES LANGUAGE CXX)
sdl_sources("${SDL3_SOURCE_DIR}/src/video/gdk/SDL_gdktextinput.cpp")
endif()
if(SDL_RENDER_D3D11 AND HAVE_D3D11_H AND NOT GDK_GAMING_XBOX)
set(SDL_VIDEO_RENDER_D3D11 1)
set(HAVE_RENDER_D3D11 TRUE)
endif()
if(SDL_RENDER_D3D12)
set(SDL_VIDEO_RENDER_D3D12 1)
set(HAVE_RENDER_D3D12 TRUE)
if(GDK_GAMING_XBOX)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/render/direct3d12/SDL_render_d3d12.c" PROPERTIES LANGUAGE CXX)
sdl_sources("${SDL3_SOURCE_DIR}/src/render/direct3d12/SDL_render_d3d12_xbox.cpp")
if(GDK_GAMING_XBOX_ONE)
sdl_sources("${SDL3_SOURCE_DIR}/src/render/direct3d12/SDL_shaders_d3d12_xboxone.cpp")
# TODO: This should also happen for "SDL_GPU && !(SDL_RENDER)"
sdl_link_dependency(d3d12 LIBS d3d12_x.lib)
elseif(GDK_GAMING_XBOX_SCARLETT)
sdl_sources("${SDL3_SOURCE_DIR}/src/render/direct3d12/SDL_shaders_d3d12_xboxseries.cpp")
sdl_link_dependency(d3d12 LIBS d3d12_xs.lib)
else()
message(FATAL_ERROR "Unknown Xbox system")
endif()
endif()
endif()
set(HAVE_SDL_VIDEO TRUE)
endif()
Expand All @@ -2065,23 +2139,34 @@ elseif(WINDOWS)

set(HAVE_SDL_THREADS TRUE)

if(SDL_SENSOR AND HAVE_SENSORSAPI_H)
if(SDL_SENSOR AND HAVE_SENSORSAPI_H AND NOT GDK_GAMING_XBOX)
set(SDL_SENSOR_WINDOWS 1)
set(HAVE_SDL_SENSORS TRUE)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/sensor/windows/*.c")
endif()

if(SDL_POWER)
set(SDL_POWER_WINDOWS 1)
if(GDK_GAMING_XBOX)
set(SDL_POWER_HARDWIRED 1)
else()
set(SDL_POWER_WINDOWS 1)
endif()
sdl_sources("${SDL3_SOURCE_DIR}/src/power/windows/SDL_syspower.c")
set(HAVE_SDL_POWER TRUE)
endif()

sdl_glob_sources("${SDL3_SOURCE_DIR}/src/locale/windows/*.c")
set(HAVE_SDL_LOCALE TRUE)

set(SDL_FILESYSTEM_WINDOWS 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/filesystem/windows/*.c")
if(NOT GDK_GAMING_XBOX)
set(SDL_FILESYSTEM_WINDOWS 1)
endif()
file(GLOB SDL3_WIN_FILESYSTEM_FILES "${SDL3_SOURCE_DIR}/src/filesystem/windows/*.c")
if(GDK_GAMING_XBOX)
list(REMOVE_ITEM SDL3_WIN_FILESYSTEM_FILES "${SDL3_SOURCE_DIR}/src/filesystem/windows/SDL_sysfilesystem.c")
sdl_sources("${SDL3_SOURCE_DIR}/src/filesystem/gdk/SDL_sysfilesystem.cpp")
endif()
sdl_glob_sources("${SDL3_WIN_FILESYSTEM_FILES}")
set(HAVE_SDL_FILESYSTEM TRUE)

set(SDL_FSOPS_WINDOWS 1)
Expand All @@ -2094,7 +2179,14 @@ elseif(WINDOWS)
set(HAVE_SDL_STORAGE 1)

# Libraries for Win32 native and MinGW
sdl_link_dependency(base LIBS kernel32 user32 gdi32 winmm imm32 ole32 oleaut32 version uuid advapi32 setupapi shell32)
if(GDK_GAMING_XBOX)
add_link_options("/NODEFAULTLIB")
# Needed to inherit base dependencies, specifically to link XblInitialize correctly
set(CMAKE_CXX_STANDARD_LIBRARIES "$(CMAKE_CXX_STANDARD_LIBRARIES) %(AdditionalDependencies) ")
sdl_link_dependency(base LIBS xgameplatform xgameruntime xmem pixevt)
else()
sdl_link_dependency(base LIBS kernel32 user32 gdi32 winmm imm32 ole32 oleaut32 version uuid advapi32 setupapi shell32)
endif()

set(SDL_TIME_WINDOWS 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/time/windows/*.c")
Expand All @@ -2109,6 +2201,10 @@ elseif(WINDOWS)
set(HAVE_SDL_LOADSO TRUE)

sdl_glob_sources("${SDL3_SOURCE_DIR}/src/core/windows/*.c")
if(GDK_GAMING_XBOX)
sdl_sources("${SDL3_SOURCE_DIR}/src/core/gdk/SDL_gdk.cpp")
# sdl_sources("${SDL3_SOURCE_DIR}/src/core/windows/pch_cpp.cpp")
endif()

if(SDL_VIDEO)
if(SDL_OPENGL)
Expand All @@ -2134,8 +2230,10 @@ elseif(WINDOWS)
endif()
endif()

sdl_glob_sources("${SDL3_SOURCE_DIR}/src/tray/windows/*.c")
set(HAVE_SDL_TRAY TRUE)
if(NOT GDK_GAMING_XBOX)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/tray/windows/*.c")
set(HAVE_SDL_TRAY TRUE)
endif()
endif()

if(SDL_HIDAPI)
Expand All @@ -2144,30 +2242,47 @@ elseif(WINDOWS)

if(SDL_JOYSTICK)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/joystick/windows/*.c")
if (GDK_GAMING_XBOX)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/joystick/windows/SDL_windowsjoystick.c" PROPERTIES LANGUAGE CXX CXX_STANDARD 17)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/joystick/windows/SDL_xinputjoystick.c" PROPERTIES LANGUAGE CXX CXX_STANDARD 17)
sdl_sources("${SDL3_SOURCE_DIR}/src/joystick/gdk/SDL_gameinputjoystick.c")
endif()

set(SDL_JOYSTICK_RAWINPUT 1)
if(NOT GDK_GAMING_XBOX)
set(SDL_JOYSTICK_RAWINPUT 1)
endif()
if(HAVE_DINPUT_H)
set(SDL_JOYSTICK_DINPUT 1)
sdl_link_dependency(joystick LIBS dinput8)
endif()
if(HAVE_XINPUT_H)
if(HAVE_XINPUT_H AND NOT GDK_GAMING_XBOX)
set(SDL_JOYSTICK_XINPUT 1)
set(HAVE_XINPUT TRUE)
endif()
if(HAVE_WINDOWS_GAMING_INPUT_H)
if(GDK_GAMING_XBOX)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/core/windows/SDL_xinput.c" PROPERTIES LANGUAGE CXX CXX_STANDARD 17)
endif()
if(HAVE_WINDOWS_GAMING_INPUT_H AND NOT GDK_GAMING_XBOX)
set(SDL_JOYSTICK_WGI 1)
endif()
if(HAVE_GAMEINPUT_H)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/joystick/gdk/*.cpp")
set(SDL_JOYSTICK_GAMEINPUT 1)
endif()
if(GDK_GAMING_DESKTOP OR GDK_GAMING_XBOX)
set(SDL_GDK_TEXTINPUT 1)
set(SDL_DISABLE_WINDOWS_IME 1)
endif()
set(HAVE_SDL_JOYSTICK TRUE)

if(SDL_HAPTIC)
if(HAVE_DINPUT_H)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/haptic/windows/*.c")
set(SDL_HAPTIC_DINPUT 1)
set(HAVE_SDL_HAPTIC TRUE)
if(GDK_GAMING_XBOX)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/haptic/windows/SDL_windowshaptic.c" PROPERTIES LANGUAGE CXX CXX_STANDARD 17)
endif()
endif()
endif()
endif()
Expand Down Expand Up @@ -3041,7 +3156,7 @@ if (SDL_DIALOG)
elseif(HAIKU)
sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/haiku/SDL_haikudialog.cc)
set(HAVE_SDL_DIALOG TRUE)
elseif(WINDOWS)
elseif(WINDOWS AND NOT GDK_GAMING_XBOX)
sdl_sources(${SDL3_SOURCE_DIR}/src/dialog/windows/SDL_windowsdialog.c)
set(HAVE_SDL_DIALOG TRUE)
elseif(MACOS)
Expand All @@ -3051,7 +3166,7 @@ if (SDL_DIALOG)
endif()

sdl_sources("${SDL3_SOURCE_DIR}/src/process/SDL_process.c")
if(WINDOWS)
if(WINDOWS AND NOT GDK_GAMING_XBOX)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/process/windows/*.c")
set(SDL_PROCESS_WINDOWS 1)
set(HAVE_SDL_PROCESS TRUE)
Expand Down Expand Up @@ -3120,6 +3235,10 @@ if(SDL_GPU)
endif()
if(WINDOWS)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/gpu/d3d12/*.c")
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/gpu/d3d12/*.cpp")
if(GDK_GAMING_XBOX)
set_source_files_properties("${SDL3_SOURCE_DIR}/src/gpu/d3d12/SDL_gpu_d3d12.c" PROPERTIES LANGUAGE CXX CXX_STANDARD 17)
endif()
set(SDL_GPU_D3D12 1)
set(HAVE_SDL_GPU TRUE)
endif()
Expand Down
Loading